【发布时间】:2018-04-06 14:38:03
【问题描述】:
在 node/express 中,我有这个
var express = require('express');
var path = require('path');
// start express module
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
/*
* Visit the home page
*/
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
var server = app.listen(process.env.PORT || 3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('My site started at http://%s:%s', host, port);
});
所以我只能使用主页链接访问我的页面,或者直接指向服务器上的某个文件。
但是在客户端,我使用带有历史 API 的单页应用程序设计,并且有类似 localhost:3000/work 的路由。因此,如果我要转到根页面,有一个按钮使用历史 API 将 url 更改为 /work 并最终显示新内容。
但是,如果我访问 localhost:3000/work maually,node.js 认为我需要找到一个名为 localhost:3000/work/index.html 的文件,并说找不到页面。如何设置让页面请求仍然触发index.html的返回?
谢谢
【问题讨论】:
标签: javascript node.js routing single-page-application html5-history