
if (!exists) {
res.writeHead(404, {
'Content-Type': 'text/plain'
})
res.write("This req URL " + pathname + " was not found on this server.")
res.end()
} else {
fs.readFile(realPath, "binary", function (err, file) {
if (err) {
res.writeHead(500, {
'Content-Type': 'text/plain'
})
res.end(err)
} else {
var contentType = "text/html"
res.writeHead(200, {
'Content-Type': contentType
})
res.write(file, "binary")
res.end()
}
})
}
})
如果是单纯静态html,可以使用express.static()中间件设定静态文件目录,然后将html文件放在入,如:express默认静态文件目录为:app.use(express.static(path.join(__dirname, 'public')))
把index.html文件放入public文件夹中,接着就可以 访问。
通常你还是需要使用模版的除了jade,ejs和paulguo的juicer都更为简单。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)