首页 > 解决方案 > 为什么我不能在本地主机上运行 server.js?

问题描述

所以我有一个带有文件 server.js 的文件夹,在 server.js 里面有这样的代码:

var http = require ('http');
    
http.createServer(function(req, res){
    console.log(req);
    console.log("***********************************");
    
    res.writeHead(200, {'Content-Type' : 'text/html'});
    res.write('Hello World');
    res.end();
}).listen(80);

同样在包含此文件的文件夹中,我安装了节点模块和 npm 以及 package.json 和 package-lock.json 但由于某种原因,当我尝试使用节点 server.json 运行它时,它并没有在端口 80 上运行,而是有时候是这样的。https://imgur.com/KIgHhBm

我怎样才能解决这个问题?我希望能够通过 chrome 打开 server.js,它应该写出 hello world

标签: javascriptnode.jsnode-modules

解决方案


推荐阅读