1.创建一个 server.js
文件:
const http = require('http');
const fs = require('fs');
const path = require('path');
const server = http.createServer((req, res) => {
// 获取请求的文件路径
const filePath = path.join(__dirname, 'dist', req.url);
// 读取文件内容并返回给客户端
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
}
});
});
const port = 3000;
server.listen(port, () => {
console.log(`Server is running at http://127.0.0.1:${port}`);
});
node server.js