接收get
var query = url.parse(request.url,true).query;
console.log('server:',pathname); // 获取路径
console.log('query1:',query.name); // 接收name(键)
console.log('query2:',query.url);
接收post
var querystring = require('querystring');
// 默认出发data事件,将post中的参数全部加入到body中
var body = "";
req.on('data', function (chunk) {
body += chunk;
});
quer = querystring.parse(body) // 创建一个对象,包含所有参数
quer.name quer.age // 获取参数值
跳转示例
## app.js
var app = require('express')();
app.set('views','template'); #设置html位置
app.engine('html', require('ejs').__express); #设置html引擎
app.get('/',function(req,res){ #设置路由为/
res.render('index.html',{ss:'asdasdas'}); #渲染模板
}
);
var server = app.listen(10000,function(){ #监听10000端口
var host = server.address().address;
var port = server.address().port;
console.log('app listening at port: ${port}'); #终端打印
});
app.get('/index',function (req,res) { #
res.send('ok')
// res.render('Template/index.html');
});
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
this is index
<%= ss %> # 接收渲染数据
<a href="/index"> index </a> # a标签跳转
</body>
</html>
为了使用response.render(‘html’,callbackfunction)
mkdir mynode #创建一个目录
npm init #创建了一个.json文件 一直回车
npm install express --save #下载express
npm install ejs #下载ejs 模版引擎