Reputation: 8315
How can I access my server through the browser once I've set up the server ?
In PHP I simply have to put my files in the www/ folder and then go to http://127.0.0.1/index.php
, but how can I do that with NodeJS ?
My server's code is from a tutorial :
var app = require(‘express’).createServer();
app.get(‘/’, function(request, response){
response.send(‘Hello Web Designer’);
});
app.listen(8888);
But whenever I go to http://127.0.0.1:8888/
i get a "Problem loading the page". So either my server is not running correctly (hard to tell when the NodeJS console shows "...") or I am not accessing it correctly.
What can I do please ?
Upvotes: 0
Views: 2534
Reputation: 746
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
Upvotes: 1
Reputation: 3400
Did you start the server? The apache server runs php, in the nodejs world, there is a javascript runtime that needs to be started.
Try node server.js
from the command line (or whatever your file is called) to get it to listen and serve incoming requests.
Upvotes: 0