node.js中express框架的用法
可以设置中间件来响应 HTTP 请求。
定义了路由表用于执行不同的 HTTP 请求动作。
可以通过向模板传递参数来动态渲染 HTML 页面。
npm install express --save
npm install body-parser --save
npm install cookie-parser --save
npm install multer --save
npm list express
语法:
app.get('/', function (req, res) {
// --
})
创建main.js实例:
- //main.js 文件
- var express = require('express');
- var app = express();
-
- app.get('/', function (req, res) {
- res.send('Hello World');
- })
-
- var server = app.listen(8080, function () {
-
- var host = server.address().address
- var port = server.address().port
-
- console.log("应用实例,访问地址为 http://%s:%s", host, port)
-
- })
- var express = require('express');
- var app = express();
-
- // 主页输出 "Hello World"
- app.get('/', function (req, res) {
- console.log("主页 GET 请求");
- res.send('Hello GET');
- })
-
-
- // POST 请求
- app.post('/', function (req, res) {
- console.log("主页 POST 请求");
- res.send('Hello POST');
- })
-
- // /del_user 页面响应
- app.get('/del_user', function (req, res) {
- console.log("/del_user 响应 DELETE 请求");
- res.send('删除页面');
- })
-
- // /list_user 页面 GET 请求
- app.get('/list_user', function (req, res) {
- console.log("/list_user GET 请求");
- res.send('用户列表页面');
- })
-
- // 对页面 abcd, abxcd, ab123cd, 等响应 GET 请求
- app.get('/ab*cd', function(req, res) {
- console.log("/ab*cd GET 请求");
- res.send('正则匹配');
- })
-
-
- var server = app.listen(8080, function () {
-
- var host = server.address().address
- var port = server.address().port
-
- console.log("应用实例,访问地址为 http://%s:%s", host, port)
-
- })
语法:
app.use('/public', express.static('public'));
创建main.js代码:
- var express = require('express');
- var app = express();
-
- app.use('/public', express.static('public'));
-
- app.get('/', function (req, res) {
- res.send('Hello World');
- })
-
- var server = app.listen(8080, function () {
-
- var host = server.address().address
- var port = server.address().port
-
- console.log("应用实例,访问地址为 http://%s:%s", host, port)
-
- })
创建index.html:
- <html>
- <body>
- <form action="http://127.0.0.1:8080/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('/public', express.static('public'));
-
- app.get('/index.html', function (req, res) {
- res.sendFile( __dirname + "/" + "index.html" );
- })
-
- app.get('/process_get', function (req, res) {
-
- // 输出 JSON 格式
- var response = {
- "first_name":req.query.first_name,
- "last_name":req.query.last_name
- };
- console.log(response);
- res.end(JSON.stringify(response));
- })
-
- var server = app.listen(8080, function () {
-
- var host = server.address().address
- var port = server.address().port
-
- console.log("应用实例,访问地址为 http://%s:%s", host, port)
-
- })
创建index.html:
- <html>
- <body>
- <form action="http://127.0.0.1:8080/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>
- body>
- html>
创建server.js:
- 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('/public', express.static('public'));
-
- app.get('/index.html', function (req, res) {
- res.sendFile( __dirname + "/" + "index.html" );
- })
-
- app.post('/process_post', urlencodedParser, function (req, res) {
-
- // 输出 JSON 格式
- var response = {
- "first_name":req.body.first_name,
- "last_name":req.body.last_name
- };
- console.log(response);
- res.end(JSON.stringify(response));
- })
-
- var server = app.listen(8080, function () {
-
- var host = server.address().address
- var port = server.address().port
-
- console.log("应用实例,访问地址为 http://%s:%s", host, port)
-
- })
创建index.html:
- <html>
- <head>
- <title>文件上传表单title>
- head>
- <body>
- <h3>文件上传:h3>
- 选择一个文件上传: <br />
- <form action="/file_upload" method="post" enctype="multipart/form-data">
- <input type="file" name="image" size="50" />
- <br />
- <input type="submit" value="上传文件" />
- form>
- body>
- html>
创建server.js:
- var express = require('express');
- var app = express();
- var fs = require("fs");
-
- var bodyParser = require('body-parser');
- var multer = require('multer');
-
- app.use('/public', express.static('public'));
- app.use(bodyParser.urlencoded({ extended: false }));
- app.use(multer({ dest: '/tmp/'}).array('image'));
-
- app.get('/index.html', function (req, res) {
- res.sendFile( __dirname + "/" + "index.html" );
- })
-
- 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
- };
- }
- console.log( response );
- res.end( JSON.stringify( response ) );
- });
- });
- })
-
- var server = app.listen(8080, function () {
-
- var host = server.address().address
- var port = server.address().port
-
- console.log("应用实例,访问地址为 http://%s:%s", host, port)
-
- })
创建main.js文件:
- // express_cookie.js 文件
- var express = require('express')
- var cookieParser = require('cookie-parser')
- var util = require('util');
-
- var app = express()
- app.use(cookieParser())
-
- app.get('/', function(req, res) {
- console.log("Cookies: " + util.inspect(req.cookies));
- })
-
- app.listen(8080)
觉得有用可以点赞或收藏!