标签:str sub location load() 格式 protoc mime cookie 接收
npm install express --save
var express = require(‘express‘);
var app = express();
app.get(‘/‘, function (req, res) {
res.send(‘Hello World‘);
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
})
req.app:当callback为外部文件时,用req.app访问express的实例
req.baseUrl:获取路由当前安装的URL路径
req.body / req.cookies:获得「请求主体」/ Cookies
req.fresh / req.stale:判断请求是否还「新鲜」
req.hostname / req.ip:获取主机名和IP地址
req.originalUrl:获取原始请求URL
req.params:获取路由的parameters
req.path:获取请求路径
req.protocol:获取协议类型
req.query:获取URL的查询参数串
req.route:获取当前匹配的路由
req.subdomains:获取子域名
req.accepts():检查可接受的请求的文档类型
req.acceptsCharsets / req.acceptsEncodings / req.acceptsLanguages:返回指定字符集的第一个可接受字符编码
req.get():获取指定的HTTP请求头
req.is():判断请求头Content-Type的MIME类型
res.app:同req.app一样
res.append():追加指定HTTP头
res.set()在res.append()后将重置之前设置的头
res.cookie(name,value [,option]):设置Cookie
opition: domain / expires / httpOnly / maxAge / path / secure / signed
res.clearCookie():清除Cookie
res.download():传送指定路径的文件
res.get():返回指定的HTTP头
res.json():传送JSON响应
res.jsonp():传送JSONP响应
res.location():只设置响应的Location HTTP头,不设置状态码或者close response
res.redirect():设置响应的Location HTTP头,并且设置状态码302
res.send():传送HTTP响应
res.sendFile(path [,options] [,fn]):传送指定路径的文件 -会自动根据文件extension设定Content-Type
res.set():设置HTTP头,传入object可以一次设置多个头
res.status():设置HTTP状态码
res.type():设置Content-Type的MIME类型
// /del_user 页面响应
app.get(‘/del_user‘, function (req, res) {
console.log("/del_user 响应 DELETE 请求");
res.send(‘删除页面‘);
})
// POST 请求
app.post(‘/‘, function (req, res) {
console.log("主页 POST 请求");
res.send(‘Hello POST‘);
})
app.use(express.static(‘public‘));
//index.htm 文件代码:
<html>
<body>
<form action="http://127.0.0.1:8081/process_get" method="GET">
First Name: <input type="text" name="first_name"> <br>
Last Name: <input type="text" name="last_name">
<input type="submit" value="Submit">
</form>
</body>
</html>
server.js 文件代码:
var express = require(‘express‘);
var app = express();
app.use(express.static(‘public‘));
app.get(‘/index.htm‘, function (req, res) {
res.sendFile( __dirname + "/" + "index.htm" );
})
app.get(‘/process_get‘, function (req, res) {
// 输出 JSON 格式
response = {
first_name:req.query.first_name,
last_name:req.query.last_name
};
res.end(JSON.stringify(response));
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("应用实例,访问地址为 http://%s:%s", host, port)
})
//index.html 文件代码:
<form action="http://127.0.0.1:8081/process_post" method="POST">
First Name: <input type="text" name="first_name"> <br>
Last Name: <input type="text" name="last_name">
<input type="submit" value="Submit">
</form>
var express = require(‘express‘);
var app = express();
var bodyParser = require(‘body-parser‘);
// 创建 application/x-www-form-urlencoded 编码解析
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(express.static(‘public‘));
app.get(‘/index.htm‘, function (req, res) {
res.sendFile( __dirname + "/" + "index.htm" );
})
app.post(‘/process_post‘, urlencodedParser, function (req, res) {
// 输出 JSON 格式
response = {
first_name:req.body.first_name,
last_name:req.body.last_name
};
res.end(JSON.stringify(response));
})
//index.htm 文件代码:
<form action="/file_upload" method="post" enctype="multipart/form-data">
<input type="file" name="image" size="50" />
<input type="submit" value="上传文件" />
</form>
//server.js 文件代码:
var express = require(‘express‘);
var app = express();
var fs = require("fs");
var bodyParser = require(‘body-parser‘);
var multer = require(‘multer‘);
app.use(express.static(‘public‘));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(multer({ dest: ‘/tmp/‘}).array(‘image‘));
app.get(‘/index.htm‘, function (req, res) {
res.sendFile( __dirname + "/" + "index.htm" );
})
app.post(‘/file_upload‘, function (req, res) {
console.log(req.files[0]); // 上传的文件信息
var des_file = __dirname + "/" + req.files[0].originalname;
fs.readFile( req.files[0].path, function (err, data) {
fs.writeFile(des_file, data, function (err) {
if( err ){
console.log( err );
}else{
response = {
message:‘File uploaded successfully‘,
filename:req.files[0].originalname
};
}
res.end( JSON.stringify( response ) );
});
});
})
// express_cookie.js 文件
var express = require(‘express‘)
var cookieParser = require(‘cookie-parser‘)
var app = express()
app.use(cookieParser())
app.get(‘/‘, function(req, res) {
console.log("Cookies: ", req.cookies)
})
app.listen(8081)
标签:str sub location load() 格式 protoc mime cookie 接收
原文地址:https://www.cnblogs.com/mapsxy/p/10569796.html