Initialize the
Create a new project
$ npm init
Copy the code
Install express
$ npm install express --save
Copy the code
Express the generator
Install the following commands
$ npm install express-generator -g
Copy the code
To create the myApp application in the current directory, run the following command
$ express myapp
$ cd myapp
$ npm install
> set DEBUG=myapp & npm start
Copy the code
Then open http://localhost:3000/ in your browser to see the application.
Applications created with Express application Generator typically have the following directory structure:
├ ─ ─ app. Js ├ ─ ─ bin │ └ ─ ─ WWW ├ ─ ─ package. The json ├ ─ ─ public │ ├ ─ ─ images │ ├ ─ ─ javascripts │ └ ─ ─ stylesheets │ └ ─ ─ Style. CSS ├── routes │ ├─ index.js │ ├─ download.js │ ├─ download.htm │ ├─ download.htm │ ├─ download.htm │ ├─ download.htm │ ├─ download.htm │ ├─ download.htm │Copy the code
Write the interface
This can be seen in index.js of routes
router.get('/'.function(req, res, next) {
res.render('index', { title: 'Express' });
});
Copy the code
The route specified home page will render the Express set by the index template title. The index template is index.jade in the views folder
Here we write a data that returns JSON
/* GET home page. */
router.get('/'.function(req, res, next) {
res.render('index', { title: 'Express' });
});
/*GET list page.*/
router.get('/list'.function(req, res, next) {
res.json({data:[],code:'200',codeStr:'Sent successfully'})
//res.render('list', { title: 'Two tablets of my Letter'}); }); /* All requests */ router.all("*",(req,res)=>{
res.json("Request error")})Copy the code
Res.json When requesting your localhost:3000/list will return res.json data returned, roter.all() here all other non-home and /list requests return a request error
Here we find that we need to re-npm run start every time we change something.
Let’s install a plugin to hot update the Node application
$ npm install -g hotnode
Copy the code
Notice here in our package.json
"scripts": {
"start": "hotnode ./bin/www"
},
Copy the code
Change node to HotNode
And then launch the program and we can have fun developing freely