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
Sometimes we want to implement such a function: c...
This article describes the Linux file management ...
【Introduction】: Handtrack.js is a prototype libra...
Understanding of diff algorithm in React diff alg...
Preface As we all know, everything in Linux is a ...
The simplest application of store in Vue is globa...
Install jdk: Oracle official download https://www...
1. MySQL self-connection MySQL sometimes needs to...
Table of contents Introduction and Demo API: Cont...
When talking about this issue, some people may ask...
MYSQL officially provides an Installer method to ...
Check the Python version python -V If it is below...
Data integrity is divided into: entity integrity,...
Use the following command to create a container a...
Use Javascript to achieve the countdown effect, f...