问题描述
我正在使用javascript
和node.js
构建应用。
当我要打开主页时,将使用express
进行此操作:
app.get('/',function(req,res){
res.sendFile(__dirname + '/index.html');
});
以这种方式,当我打开主页时,它将返回我index.html
但是我想知道如何仅使用Nodejs
而不使用express
做同样的事情。
我想我需要先创建服务器,然后在请求为index.html
时以某种方式返回/
页面,尽管我不确定这是否正确。
const server = http.createServer((req,res) => {
res.setHeader("Access-Control-Allow-Origin","*");
});
server.on('request',(request,response)=> {
if (requsest.url == '/') {
response.end('index.html')
}
});
上面的代码是我的思考方式,可能不是正确的方式,因此,如果有人可以给我想法或解决方案,请问我该如何在Nodejs
中进行操作。
预先感谢:)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)