Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js:如何将默认页面设置为index.html以外的其他页面_Node.js - Fatal编程技术网

Node.js:如何将默认页面设置为index.html以外的其他页面

Node.js:如何将默认页面设置为index.html以外的其他页面,node.js,Node.js,最初,默认情况下,我的node.js服务器转到index.html。 现在我想将默认值设置为login.html,这样人们就可以先登录 我的代码在../server/server.js中,而客户端页面在../client/login.html,index.html等中 现在我修改了server.js如下: app.get('/', function(req, res) { res.sendfile(path.resolve('../client/login.html')); }); 重

最初,默认情况下,我的node.js服务器转到
index.html
。 现在我想将默认值设置为
login.html
,这样人们就可以先登录

我的代码在
../server/server.js
中,而客户端页面在
../client/login.html
index.html
等中

现在我修改了
server.js
如下:

app.get('/', function(req, res)
{
    res.sendfile(path.resolve('../client/login.html'));
});

重新启动server.js后,默认情况下网页仍指向
index.html
。我遗漏了什么?

如果您在Nodejs之上运行ExpressJS,那么可以使用static方法静态地为文件提供服务。第一个参数是目录,第二个参数允许您指定默认文件

app.use(express.static('../client/', {index: 'login.html'}))

对于您的特定示例,您可以修改sendFile,将root包含在第二个参数中:

res.status(200).sendFile('login.html', { root: path.join(__dirname, '../client/') });

如果您还有一个路由器正在处理为您获取索引页的操作,并且您希望呈现把手页或执行其他操作,则可以将任何内容放入索引选项中,如果在静态资产文件夹中找不到,它将忽略该内容:

app.use(
    express.static(
        path.resolve(__dirname, "../Client/assets/"),
        {index: "userouterinstead"}
    )
)

app.use("/", route_configs)

app.get("*",  (req, res, next) => {
    res.sendFile(
        path.resolve( __dirname, "../Client/assets/index.html" )
    )
})

非常感谢。