【发布时间】:2018-06-10 10:09:19
【问题描述】:
以下两个代码在服务器启动时在 localhost:3000/ 处提供 index.html。
express.static的使用
const path = require('path');
const express = require('express');
const PORT = process.env.port || 3000;
const publicPath = path.join(__dirname, '../public');
var app = express();
app.use(express.static(publicPath));
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
})
app.get的使用
const path = require('path');
const express = require('express');
const PORT = process.env.port || 3000;
const publicPath = path.join(__dirname, '../public');
var app = express();
app.get('/',(req,res) => {
res.sendFile(publicPath + '/index.html');
})
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
})
那么为什么有人会选择express.static 而不是app.get 来提供静态html 文件。快递上static中间件有什么用
【问题讨论】: