Pic*_*cci 11 httpserver node.js
我已在http-server全球安装.
我在localhost端口8080上从myDir启动它.在myDir中我有index.html.
如果我(从浏览器)请求http://localhost:8080/我得到index.html,这是好的.
如果我请求虽然http://localhost:8080/anything我没有从服务器得到任何响应.
相反,我想要的是我的服务器总是使用index.html响应任何到达端口8080上的localhost的http请求.
这可能吗.
提前致谢
Far*_*rov 10
按照文档中的说明使用。
http-server --port 8080 -P http://localhost:8080?
Run Code Online (Sandbox Code Playgroud)
请注意?代理 URL 末尾的 。
使用Express 4.x的简单直接示例:
var express = require('express');
var app = express();
var path = __dirname + '/public';
var port = 8080;
app.use(express.static(path));
app.get('*', function(req, res) {
res.sendFile(path + '/index.html');
});
app.listen(port);
Run Code Online (Sandbox Code Playgroud)
index.html如果找不到所请求的文件,此实现将始终响应,并且它几乎与使用时一样简单http-server,缺少此选项.
是的,有-P/--proxy选项:
http-server -P http://localhost:8080/
Run Code Online (Sandbox Code Playgroud)
请注意,包括 404 在内的任何错误都将重定向到您的索引,而不仅仅是缺少路径。
为了实现您的要求,我建议您使用live-server而不是http-server.
live-server --port=8080 --entry-file=./index.html
Run Code Online (Sandbox Code Playgroud)
live-server也提供热重载,但这不是你的要求之一
编辑:实时服务器不适用于生产.例如,没有gzip压缩
编辑2:http-server的维护者在此评论中明确表示从不,http-server会考虑SPA用例
编辑3:发球似乎也是一个不错的选择
| 归档时间: |
|
| 查看次数: |
10031 次 |
| 最近记录: |