Javascript 所有的请求都会说;text/html“;,即使我设置了res.contentType

Javascript 所有的请求都会说;text/html“;,即使我设置了res.contentType,javascript,html,node.js,express,content-type,Javascript,Html,Node.js,Express,Content Type,所以,我运行了这个简单的小Express应用程序,它决定是否添加“.html”。这就是剧本的基本功能。以下是脚本: var-app=require(“express”)(), cproc=要求(“子流程”), http=require(“http”).Server(应用程序), fs=要求(“fs”), mime=require(“mime”) __webdir=\uu dirname+“/web”, mcs=null; 应用程序获取(“*”,函数(请求,恢复){ var getloc=uu w

所以,我运行了这个简单的小Express应用程序,它决定是否添加“.html”。这就是剧本的基本功能。以下是脚本:

var-app=require(“express”)(),
cproc=要求(“子流程”),
http=require(“http”).Server(应用程序),
fs=要求(“fs”),
mime=require(“mime”)
__webdir=\uu dirname+“/web”,
mcs=null;
应用程序获取(“*”,函数(请求,恢复){
var getloc=uu webdir+req.originalUrl,
llcurl=getloc.split(“/”[getloc.split(“/”).length-1],
modloc=null;
如果(llcurl.match(/\./g)){
modloc=getloc;
}else if(llcurl==“”){
modloc=getloc+“start.html”;
}否则{
modloc=getloc+“.html”;
}
res.send(fs.readFileSync(modloc.toString());
res.contentType(mime.lookup(modloc));
});
虽然当我使用HTML包含CSS文件或JavaScript文件时,如下所示:

<link href="main.css" rel="stylesheet" type="text/css" /> 
尽管
mime.lookup
确实返回mime类型,但在Chrome的网络菜单中,所有内容仍然是
text/html

用于理解的额外信息

因此,我的根目录是
node\u base
,其中存储了
app.js
。。。所以它位于
node\u base/app.js

HTML文件存储在
节点\u base/web/


JS和CSS文件位于
节点\u base/web/static/

不使用express.static有什么特殊原因吗?这是为了服务静态文件。@SecondRikudo对不起,对NodeJS和Express来说是个新概念。我会看医生的,你不必向我道歉。我不是在评判你:)两个建议:流式传输文件,而不是首先将其全部加载到内存中,并在开始将数据写入响应流之前将
res.contentType()
移动到。@msdex,对,我让它全部工作,因为我重新编辑了所有内容。但是,如果你愿意,你可以写下来作为答案。
res.contentType(mime.lookup(modloc));