1. Install the express library and generator Open cmd and enter the command:
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 projectEnter the command: express nodejs (successful as shown below) The directory after success: 3. Jump to the package.json directory to install related packagesEnter the command: yarn or cnpm i or npm i 4. Start the projectEnter 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
Reference link: https://www.jb51.net/article/209247.htm 6. How to develop in this projectFirst, 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 serviceInstall nodemon module Enter the command: 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
The code has been put in my github repository, attached link: github.com/wuguanfei/n… SummarizeThis 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:
|
<<: MySql uses skip-name-resolve to solve the problem of slow external network connection client
>>: Detailed explanation of log processing of Docker containers
Let me first explain why the text is not vertical...
Technology Fan html web page, you must know vue f...
This article shares the specific code of jQuery t...
Table of contents Overview Install Gulp.js Create...
1. KVM virtual machine migration method and issue...
1. Overview of viewport Mobile browsers usually r...
Recently I need to change the version of MySQL da...
0. Introduction What is the ibdata1 file? ibdata1...
filter is generally used to filter certain values...
Copy code The code is as follows: <span style=...
1. Introduction (1) Introduction to vw/vh Before ...
animation Define an animation: /*Set a keyframe t...
View installation and uninstallation # View rpm -...
Preface It is very simple to create a server in n...
1. Error error connecting to master 'x@xxxx:x...