express 路由
主要通以下方法去进行去区分url,从而对不同模块进行分别请求处理
- app.get('/', (req,res)=>{res.send()})
- app.post('/', (req,res)=>{res.send()})
- app.pust('/', (req,res)=>{res.send()})
- app.delete('/', (req,res)=>{res.send()})
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('hello world');// res.write + res.end
})
app.post('/', (req, res) => {
res.write('post hello world');
res.end()
})
app.put('/', (req, res) => {
res.send('put hello world')
})
app.delete('/', (req, res) => {
res.send('delete hello world')
})
app.listen(3000, () => {
console.log('localhost start 3000 ...')
})
Express 静态资源文件路由
- 通过express.static(sourceUrl)指定静态资源目录
- 通过app.use()完成路由配置
const express = require('express');
const path = require('path');
const app = express();
//配置多个静态资源目录
//访问:http://localhost:3000/loading.gif
app.use(express.static('public/images'));
// app.use(express.static('public/files'));
//配置多级目录
//访问:http://localhost:3000/static/1.png
app.use("/static",express.static('public/images'))
//配置绝对路径
//访问:http://localhost:3000/static/loading.gif
app.use('/static',express.static(path.resolve(__dirname,'public/files')));
app.listen(3000,()=>{
console.log("server run at port 3000...");
});