【发布时间】:2016-09-14 21:50:59
【问题描述】:
var express = require("express");
var app = express();
var path = require("path");
app.use(express.static(__dirname + '/view'));
app.get('/dashboard',function(req,res){
res.sendFile((path.join(__dirname + '/dashboard.html'));
});
app.get('/index1',function(req,res){
res.sendFile((path.join(__dirname+'/index.html'));
});
app.get('/',function(req,res){
res.redirect('/login');
});
app.get('/login',function(req,res){
res.redirect((path.join(__dirname + '/login'));
});
app.listen(3000);
console.log("Running at Port 3000");
我的问题是为什么每次用户请求时我都需要检查?
另外,如果我的目录中有100个html文件,我需要通过get方法检查每个文件,然后通过res.sendFile返回页面吗?
【问题讨论】:
-
这就是所有 Web 服务器的工作方式。当您浏览目录时,甚至您的操作系统也以这种方式工作。是什么让您认为有一种神奇的方法可以在不检查用户输入的内容的情况下确定用户想要什么?
-
虽然你说的完全正确@slebetman,但我认为 OP - 出于某种原因 - 认为他们必须列出每个静态文件,而不是创建一个服务于中间件的文件。
-
@ClemensHimmer:啊。我看到了静态中间件,以为他在问为什么静态中间件需要检查每个请求的url。因为他的问题是“每次用户请求什么”,而不是每条服务路径。我的错。