0

设置服务器后,如何通过浏览器访问我的服务器?

在 PHP 中,我只需将文件放在 www/ 文件夹中,然后转到http://127.0.0.1/index.php,但如何使用 NodeJS 做到这一点?

我的服务器代码来自教程

var app = require(‘express’).createServer();

app.get(‘/’, function(request, response){
    response.send(‘Hello Web Designer’);
});

app.listen(8888);

但是每当我去时,http://127.0.0.1:8888/我都会收到“加载页面的问题”。因此,要么我的服务器运行不正确(很难判断 NodeJS 控制台何时显示“...”),要么我没有正确访问它。

请问我能做什么?

4

2 回答 2

1

you have to send a http head and end the response.

Try this code.

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('hello world');
  res.end();
}).listen(8888);

(Pseudo Code)

Also don't use any frameworks to learn how node works. You can run you index.js now and call localhost:8888

于 2013-07-21T10:50:10.450 回答
0

你启动服务器了吗?apache服务器运行php,在nodejs的世界里,有一个需要启动的javascript运行时。

尝试node server.js从命令行(或任何你的文件被调用)让它监听和服务传入的请求。

于 2013-07-21T10:45:30.460 回答