一、node.js安装
Mac:https://cdn.npm.taobao.org/dist/node/v14.2.0/node-v14.2.0.pkg
二、打印hello word
1、创建一个文件夹。
2、创建一个文件index.js。
3、执行命令创建package.json文件,输入相关内容,回车。
命令:npm init
含义:
- name 项目名称
- version 项目的版本号
- description 项目的描述信息
- entry point 项目的入口文件
- test command 项目启动时脚本命令
- git repository 如果你有 Git 地址,可以将这个项目放到你的 Git 仓库里
- keywords 关键词
- author 作者叫啥
- license 项目要发行的时候需要的证书,平时玩玩忽略它
3、安装express(node.js Web 应用框架)
命令:npm install express
4、安装body-parser(node.js 中间件,用于处理 JSON, Raw, Text 和 URL 编码的数据)
命令:npm install body-parser
5、编写代码
文件名称:index.js
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain;charset=utf-8"});
response.write("Hello");
response.end();
}).listen(8888);
6、切换到项目根目录下执行代码
node index.js
7、浏览器访问
http://localhost:8888/
三、get请求返回数据
1、index.js代码
//引入node.js Web 应用框架
const express = require('express');
const app = express();
//引入bodyParser 方法
//node.js 中间件,用于处理 JSON, Raw, Text 和 URL 编码的数据
const bodyParser = require('body-parser');
//返回的对象为任意类型
app.use(bodyParser.urlencoded({extended:false}))
//返回一个只解析 json 的中间件,最后保存的数据都存放在 request.body 对象上
app.use(bodyParser.json())
//定义静态资源路径文件夹
app.use('/static',express.static('./static'));
// 设置跨域访问
app.all("*", function(request, response, next) {
response.header("Access-Control-Allow-Origin", "*"); // 设置跨域的域名,* 代表允许任意域名跨域
response.header("Access-Control-Allow-Headers", "X-Requested-With");
response.header(
"Access-Control-Allow-Methods",
"PUT,POST,GET,DELETE,OPTIONS"
);
response.header("X-Powered-By", " 3.2.1");
response.header("Content-Type", "application/json;charset=utf-8");
next();
});
//定义端口,此处所用为8888端口,可自行更改
var server = app.listen(8888,function(){
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s',host,port);
})
var infor = [
{
name:'jay',
age:20,
sex:'男',
hobby:'basketball'
},
{
name:'孙尚香',
age:23,
sex:'女',
hobby:'shopping'
},
{
name:'高渐离',
age:24,
sex:'男',
hobby:'music'
},
{
name:'墨子',
age:28,
sex:'男',
hobby:'game'
},
{
name:'Tony',
age:24,
sex:'男',
hobby:'no'
},
]
app.get('/api',function(req,res){
res.status(200),
res.json(infor)
})
2、执行命令
命令:node index.js
3、浏览器访问
http://localhost:8888/api
四、post请求
1、index.js代码
//引入express框架
const express = require("express")
const app = express()
/* 引入body-parser */
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.listen(8888, () => {
console.log('——————————服务已启动——————————');
})
app.post('/post',function(request,response){
response.send(request.body);
}
);
2、执行命令
命令:node index.js
3、访问post,如下:
五、文件上传
1、下载multer包
命令:npm install multer -save
2、单文件上传
js文件:upload.js
const express = require('express');
const multer = require('multer');
const app = express();
app.listen(8888);
//设置上传目录
const upload = multer({dest:'static/'});
app.post('/upload', upload.single('logo'), function(req, res, next){
let file = req.file;
console.log('文件类型:%s', file.mimetype);
console.log('原始文件名:%s', file.originalname);
console.log('文件大小:%s', file.size);
console.log('文件保存路径:%s', file.path);
res.send({ret_code: '0'});
});
网页:index.html
<html>
<head>
<meta charset="utf-8">
<title>首页</title>
</head>
<body>
<div>首页</div>
<form action="/upload" method="post" enctype="multipart/form-data">
<h2>单图上传</h2>
<input type="file" name="logo">
<input type="submit" value="提交">
</form>
</body>
</html>
3、多个文件上传
js文件:
const express = require('express');
const multer = require('multer');
const app = express();
app.listen(8888);
//设置上传目录
const upload = multer({dest:'static/'});
app.post('/form', upload.array('logo', 8), function (req, res, next) {
var paths = [];
for (var i = 0; i < req.files.length; i++) {
paths.push(req.files[i].originalname);
console.log(req)
}
res.status(200).json({
httpCode: 200,
message: '上传成功',
fileurls: paths,
});
});
网页:index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表单</title>
</head>
<body>
<form action="/form" method="post" enctype="multipart/form-data">
<h2>多图上传</h2>
<input type="file" name="logo">
<input type="file" name="logo">
<input type="file" name="logo">
<input type="submit" value="提交">
</form>
</body>
</html>
在使用中,如果仅需要处理multipart表单中的文本字段,可以使用multer中的.single()、.array()或fields()方法。
六、文件下载
后续更新……