The Web development framework Express is created as an EJS template

Express ejS ejsItem //ejsItem your project nameCopy the code


Switch to your project directory and execute Install

cd ejsItem


Copy the code


Perform installation of dependent modules

npm install
Copy the code


Installation:


Results:


Execution run server

node app.js

Copy the code

I found I couldn’t start it


The reason is that app.js in Express has removed the default listening port in order to give developers more customization. Add a listener for the port at the end of the app.js file.

// module.exports = app; // module.exports = app; app.set('port', 3000); // Listen to port 3000 app.listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
});

Copy the code


Start again and access is complete. ####2. Supervisor Startup Project To facilitate debugging and installation of the server code, install the Supervisor and start the project through the Supervisor to realize real-time update of the changes of the server code.

npm  install supervisor -g
Copy the code

Activation:

supervisor  app.js
Copy the code



####3. Simple analysis of default routes Open the app.js file

Var index = require('./routes/index');
var users = require('./routes/users');
var can = require('./routes/can'); // This is what I added.... // Use the relevant view template app.use('/', index);
app.use('/users', users);
app.use('/can', can); // I added this sentenceCopy the code

Routing control control


The view controls


Index routing (or controller) code:

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

/* GET home page. */
router.get('/'.function(req, res, next) {// Specify which view template res.render('index', { title: 'home' });
});

module.exports = router;

Copy the code

Users.js routing (or controller) code analysis:

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

/* GET users listing. */
router.get('/'.function(req, res, next) {// Return a text directly. res.send('respond with a resource');
});

module.exports = router;

Copy the code

Newly added routes and configuration of views can.js and can.ejs app.js

var can = require('./routes/can');

app.use('/can', can);
Copy the code

Results:


1. Create a new controller, that is, can.js 2. Create a new view template, aka can.ejs. 3. Introduce a controller in the entry so that you can display different pages by visiting different addresses. For example, you can create home, about, and so on, and then you have your website page basically finished, and the rest is to stuff the data into the hole.