The whole process of node.js using express to automatically build the project

The whole process of node.js using express to automatically build the project

1. Install the express library and generator

Open cmd and enter the command: yarn global add express express-generator

explain:
The two modules above represent the library and the generator respectively. In express3, installing express will automatically install the generator express-generator for you, but in express4, they are separated, so they need to be installed separately.

After installation, you can use the command: express --version to check whether the installation is successful.

The installation is successful when the version number appears (as shown in the figure below).

2. The express generator automatically creates an express project

Enter the command: express nodejs (successful as shown below)

The directory after success:

3. Jump to the package.json directory to install related packages

Enter the command: yarn or cnpm i or npm i

4. Start the project

Enter the command: npm run start

Open the browser and access 127.0.0.1:3000 to get access to our project

V. Explanation of the Project Catalog

bin: stores executable files
public: stores js, css, img and other files
router: stores routing files
views: store view files or template files
app.js: startup file (entry file)
package.json: stores project information and module dependencies. When you add dependent modules to dependencies and run npm install, npm will check the package.json in the current directory and automatically install all specified modules.
node_modules: stores the modules installed in package.json. When you add dependent modules to package.json and install them, they are stored in this folder.

Reference link: https://www.jb51.net/article/209247.htm

6. How to develop in this project

First, create a new test.js file in routes

var express = require('express');
var router = express.Router();

router.get('/', function (req, res, next) {
	res.send('I am the interface return value');
});

module.exports = router;

Then add the following code in app.js

var testRouter = require('./routes/test');
app.use('/test', testRouter);

Then open the browser console and use fetch to request the interface we just wrote

fetch('http:localhost:3000/test')
.then(res=>{
  return res.text()
}).then(res=>{
  console.log(res)
})

We found that there was a cross-domain problem, which was caused by not adding cross-domain in nodejs.

Add the following cross-domain code to app.js

//Set cross-domain access (set it before all requests)
app.all("*", function (req, res, next) {
	//Set the domain name allowed to cross domain, * represents allowing any domain name to cross domain res.header("Access-Control-Allow-Origin", "*");
	//Allowed header types res.header("Access-Control-Allow-Headers", "content-type");
	//Cross-domain allowed request methods res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
	if (req.method == 'OPTIONS')
		res.sendStatus(200); //Let options try to request a quick end else
		next();
});

Then restart the project so that you can access it normally

If we modify the content in the project, we need to restart the project manually, which is a bit troublesome. We can solve this problem by installing a plug-in.

7. Use nodemon to automatically restart the service

Install nodemon module

Enter the command: npm i nodemon -S

Create nodemon.json file

Create a nodemon.json file in the root directory of the project:

{
	"restartable": "rs",
	"ignore": [".git", ".svn", "node_modules/**/node_modules"],
	"verbose": true,
	"execMap": {
		"js": "node --harmony"
	},
	"watch": [],
	"env": {
		"NODE_ENV": "development"
	},
	"ext": "js json njk css js"
}

Using the nodemon module

In your package.json file, add a line of script code

"dev": "nodemon ./bin/www"

The code has been put in my github repository, attached link: github.com/wuguanfei/n…

Summarize

This is the end of this article about how to automatically build a project with node.js and express. For more information about how to automatically build a project with node.js express, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Node koa2 ssr project construction steps
  • Simple deployment of nodeJs project in Alibaba Cloud
  • How to deploy Node.js project on cloud server (novice series)
  • Detailed steps to create a Vue project in node
  • Node command line tool to achieve the standard process of automatic initialization of project engineering
  • How to create gitkeep for all empty folders in nodejs project
  • Build the vueSSR project from 0 to 1: node and vue-cli3 configuration
  • How to unit test nodejs projects in PHPStorm
  • How to use pm2 to automatically deploy node projects
  • Why node.js is not suitable for large projects

<<:  MySql uses skip-name-resolve to solve the problem of slow external network connection client

>>:  Detailed explanation of log processing of Docker containers

Recommend

One minute to experience the smoothness of html+vue+element-ui

Technology Fan html web page, you must know vue f...

jQuery implements Table paging effect

This article shares the specific code of jQuery t...

How to implement online hot migration of KVM virtual machines (picture and text)

1. KVM virtual machine migration method and issue...

The basic principles and detailed usage of viewport

1. Overview of viewport Mobile browsers usually r...

MySQL FAQ series: How to avoid a sudden increase in the size of the ibdata1 file

0. Introduction What is the ibdata1 file? ibdata1...

Detailed explanation of application scenarios of filters in Vue

filter is generally used to filter certain values...

CSS3 mobile vw+rem method to achieve responsive layout without relying on JS

1. Introduction (1) Introduction to vw/vh Before ...

Common styles of CSS animation effects animation

animation Define an animation: /*Set a keyframe t...

Detailed explanation of rpm installation in mysql

View installation and uninstallation # View rpm -...

Detailed explanation of the steps to create a web server with node.js

Preface It is very simple to create a server in n...

Explanation of the precautions for Mysql master-slave replication

1. Error error connecting to master 'x@xxxx:x...