1.1.1 Routing configuration
The purpose of routing: eight words, loading the page, displaying data (data in the interface).
node routing, purpose:Load page. The page takes the data from the interface and displays it on the page.
Create a cms project
Create the routers folder, create the index.js routing configuration file, and instantiate the routing class router in nodejs to access the page through the path.
1.1.1.1 home.js frontend routing configuration file
Under the routers folder, index.js front-end routing configuration file
req, res, next description:
req request object saves the relevant information requested by the client res response object the response information returned by the server next executes the next method
// 导入express框架
let express=require('express')
// 创建express服务器实例
let app=express();
//实例化路由对象
let router=express.Router()
// 前台首页页面路由
router.get('/home',function(req,res,next){
// res.send('访问首页页面')
// 载入页面
res.render('home/index.html')
})
module.exports=router;
1.1.1.2 admin.js background routing configuration file
Under the routers folder, admin.js background routing configuration file
let express=require('express')
let app=express();
//实例化路由对象
let router=express.Router()
// 后台登录页面路由
router.get('/login',function(req,res,next){
// res.send('访问登录页面')
res.render('admin/login.html')
})
module.exports=router;
1.1.1.3 Using foreground routing
The app.js file uses foreground routing
//导入express框架
let express=require('express')
//初始化
let app=express();
app.get('/',function(req,res){
res.send('hi nodejs!')
})
//导入前台路由和后台路由
let adminRouter=require('./routers/admin')
let homeRouter=require('./routers/home');
// 使用前台路由
// 参数1:匹配路由规则
// 参数2:请求路由规则
app.use('/',homeRouter)
app.use('/admin',adminRouter)
module.exports = router;
1.1.2 Common module configurations for app.js entry files
body-parser module: handle post requests
express-session module: handle session
ejs and path modules: Set template engine related information
express.static(): Set access to static resources
Load ueditor module: var ueditor = require("ueditor");
Import moment module: const moment = require("moment"); , nodejs moment operation time
req, res, next description:
req request object saves the relevant information requested by the client res response object the server enters the corresponding next to execute the next method
Commonly used methods in res:
res.redirect('/public/baidu/php/config.json'); //Load the file res.render("home/index"); //Load the page
res.send("I am the homepage of the blog project"); // Output a piece of text to the page
//导入express框架
let express=require('express')
//初始化
let app=express();
app.get('/',function(req,res){
res.send('hi nodejs!')
})
//设置模板引擎相关信息
let ejs=require('ejs')//需要下载
let path=require('path')//nodejs底层自带模块
app.set('views','./views');
app.engine('html',ejs.__express)
// 设置静态资源的访问
app.use('/public',express.static(__dirname+'/public'))
app.use('/upload',express.static(__dirname+'/upload'))
app.use('/images',express.static(__dirname+'/images'))
//导入前台路由和后台路由
let adminRouter=require('./routers/admin')
let homeRouter=require('./routers/home');
// 使用前台路由
// 参数1:匹配路由规则
// 参数2:请求路由规则
app.use('/',homeRouter)
app.use('/admin',adminRouter)
//监听服务器
let port=3000
app.listen(port,function(){
console.log('node服务器已经启动,端口是:'+port);
})
login.html in the corresponding admin folder
<h1>后台登录页面</h1>
Index.html in the corresponding home folder
<h1>我是前台首页</h1>
Related preview: