nodejs让路由继续往下匹配怎么写
2017-08-02 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
实例是模拟登录页面提交表单,然后根据信息判断是否登录成功
login.js
var express =require('express');
var router =express.Router();
router.get('/',function(req,res,next){
res.render("login.jade");
});
module.exports = router;
login.jade
doctype html
html
head
meta(http-equiv="Content-Type",content="text/html; charset=utf-8")
title 添加数据页
body
h1 请填写你要发送的信息
form(action="/loginmsg",method="post")
input(name="msg",type="text",id="msg")
input(name="submit",type="submit")
loginmsg.js
var express =require('express');
var router =express.Router();
router.post('/',function(req,res,next){
var msg = req.body.msg;
if(msg){
res.render("loginmsg.jade",{msg:msg});
}
});
module.exports = router;
loginmsg.jade
doctype html
html
head
title 主页
body
div 你发送的信息为:#{msg}
app.js
var express = require('express');
var path = require('path');
var app = express();
var bodyParser = require('body-parser');
var login = require('./routes/login');
var loginmsg = require('./routes/loginmsg');
//模板引擎设置
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
//设置路由
app.use('/',login);
app.use('/loginmsg',loginmsg);
app.listen(3000);
console.log("启动端口:3000");
login.js
var express =require('express');
var router =express.Router();
router.get('/',function(req,res,next){
res.render("login.jade");
});
module.exports = router;
login.jade
doctype html
html
head
meta(http-equiv="Content-Type",content="text/html; charset=utf-8")
title 添加数据页
body
h1 请填写你要发送的信息
form(action="/loginmsg",method="post")
input(name="msg",type="text",id="msg")
input(name="submit",type="submit")
loginmsg.js
var express =require('express');
var router =express.Router();
router.post('/',function(req,res,next){
var msg = req.body.msg;
if(msg){
res.render("loginmsg.jade",{msg:msg});
}
});
module.exports = router;
loginmsg.jade
doctype html
html
head
title 主页
body
div 你发送的信息为:#{msg}
app.js
var express = require('express');
var path = require('path');
var app = express();
var bodyParser = require('body-parser');
var login = require('./routes/login');
var loginmsg = require('./routes/loginmsg');
//模板引擎设置
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
//设置路由
app.use('/',login);
app.use('/loginmsg',loginmsg);
app.listen(3000);
console.log("启动端口:3000");
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询