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

Handtrack.js library for real-time monitoring of hand movements (recommended)

【Introduction】: Handtrack.js is a prototype libra...

In-depth analysis of the diff algorithm in React

Understanding of diff algorithm in React diff alg...

How to Understand and Identify File Types in Linux

Preface As we all know, everything in Linux is a ...

Detailed explanation of Vue's simple store

The simplest application of store in Vue is globa...

Install tomcat and deploy the website under Linux (recommended)

Install jdk: Oracle official download https://www...

In-depth understanding of MySQL self-connection and join association

1. MySQL self-connection MySQL sometimes needs to...

Detailed explanation of the use of Vue Smooth DnD, a draggable component of Vue

Table of contents Introduction and Demo API: Cont...

Issues with upgrading Python and installing Mongodb drivers under Centos

Check the Python version python -V If it is below...

MySQL: Data Integrity

Data integrity is divided into: entity integrity,...

Implementing countdown effect with javascript

Use Javascript to achieve the countdown effect, f...