节点服务器如何运行

时间:2015-08-14 18:51:47

标签: node.js

我有我的服务器,但我该如何运行它?我希望看到“欢迎使用Node Essential Training”。

var http = require('http');

    var handleRequest = function (req, res) {
            res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Welcome to Node Essential Training/n');
        };

var server = http.createServer(handleRequest);
server.listen(3000. 'localhost');
             }

2 个答案:

答案 0 :(得分:1)

打开终端,将路径更改为保存服务器文件的位置,假设文件名是server.js,只需在终端输入以下命令:node server.js,然后按回车键。

之后转到任何浏览器并输入网址为http://localhost:3000,您将看到输出。

答案 1 :(得分:1)

你最后有一个额外的括号

同样纠正最后一行

server.listen(3000);

现在将文件作为

运行
node <your file name>

您可以在http://localhost:3000

看到您的网站