【发布时间】:2019-02-18 23:33:57
【问题描述】:
我想通过 express js 服务于 vue js dist/。我在 vue js 应用程序中使用历史路由器。
以下是api调用
- api/
- s-file/sending/:id
- terms/get/:which
正如我在 python here 中找到的解决方案一样。我不知道如何在带有express的node js中做到这一点
我现在使用的代码是
app.use(function (req, res, next) {
if (/api/.test(req.url))
next();
else {
var file = "";
if (req.url.endsWith(".js")) {
file = path.resolve(path.join(distPath, req.url))
res.header("Content-Type", "application/javascript; charset=utf-8");
res.status(200);
res.send(fs.readFileSync(file).toString());
} else if (req.url.endsWith(".css")) {
file = path.resolve(path.join(distPath, req.url))
res.header("Content-Type", "text/css; charset=utf-8");
res.status(200);
res.send(fs.readFileSync(file).toString());
} else {
file = path.resolve(path.join(distPath, "index.html"))
res.header("Content-Type", "text/html; charset=utf-8");
res.status(200);
res.send(fs.readFileSync(file).toString());
}
}
})
【问题讨论】:
-
我不清楚你的要求,是 api/ 是一个 rest api 还是有任何静态文件目录等。?
-
所有提到的端点都是 REST API