Creating an Express Project, Defining Routes, and Using next() for Control Transfer
This guide walks through installing Express and express‑generator, creating a new Express project with EJS templates, adding custom routes and route parameters, and using the next() function for middleware‑style control transfer, illustrating each step with concrete code snippets and command‑line examples.
Scenario
npm provides many third‑party modules, including many web frameworks. Express is chosen because it is stable, widely used, and the only web framework officially recommended by Node.js.
Implementation
Install Express
Install Express and the express‑generator tool globally:
npm install -g express
npm install -g express-generatorRun express --help to view usage.
Create a new Express project
Generate a project named microblog with the EJS template engine: express -t ejs microblog Enter the directory and install dependencies:
cd microblog
npm installAdd a listening port in app.js:
app.listen(3000,function () {
console.log('服务端启动在3000端口')
})Start the server with node app.js and open http://localhost:3000.
Add custom routes
Declare a new router for path /badao in app.js:
app.use('/badao', badaoRouter);
var badaoRouter = require('./routes/badao');Create routes/badao.js:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.send('这是来自badao--公众号:霸道的程序猿');
});
module.exports = router;After restarting, http://localhost:3000/badao returns the response.
Route parameter matching
In routes/index.js add a parameterized route:
router.get('/hello/:username',function (req,res) {
res.send('Hello:'+req.params.username);
});Visiting http://localhost:3000/hello/YourName displays a personalized greeting.
Control transfer with next()
Express matches routes in definition order; later routes are ignored if an earlier one handles the request. Use the third next argument to pass control:
router.all('/:username',function (req,res,next) {
if(users[req.params.username]) {
next();
} else {
next(new Error(req.params.username+'不存在'));
}
});
router.get('/:username',function (req,res) {
res.send(JSON.stringify(users[req.params.username]));
});This pattern validates the username parameter before returning the user object, demonstrating middleware and control transfer.
Example code download
Full source code is available at https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12969105
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
The Dominant Programmer
Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
