Executing javascript code with node.js - javascript

I'm trying to execute a javascript code with node.js, and I get always two errors saying :
.port 1 is not active
.port 2 is not active
This my javascript code :
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
Any ideas ?

Do you have anything else listening on 8124 already?
netstat -an|grep :8124

Related

read and print file contents with nodejs server?

hello I'm using docker and ansible to launch a container which will then launch a server.js file, which needs to display the contents of an index.html file located in the same directory. I have this skeleton outline code which works in displaying "Hello World" to the screen when I curl the ip address running this server.js file
var http = require('http');
http.createServer(function (request, response)
{
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8080);
console.log('Server started');
the console.log does not display on the screen, seemingly only response.end does that, with printing 'Hello World' to the screen, so I've been trying to read in the index.html file as a variable and have response.end display it but with no luck.
I tried doing this:
var http = require('http');
http.createServer(function (request, response)
{
response.writeHead(200, {'Content-Type': 'text/plain'});
var fs = require('fs');
var readStream = fs.createReadStream('index.html');
readStream.pipe(response);
//response.end('Hello World\n');
}).listen(8080);
console.log('Server started');
but when I tried to curl it it resulted in a an Error 52 empty reply from the server, am I not doing enough to read in my index.html file and store it as a string variable? thanks.
var http = require('http');
var fs = require("fs");
http.createServer(function(req, res) {
fs.readFile("index.html","utf8" ,function(err, contents){
console.log(contents);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(contents);
res.end();
});
}).listen(3000);
if you want to show the contents of the file on request to the server try this.

Node.js & Express.js Font Differentiation

I developed examples on Node.js and Express.js arbtrarily. After initiating example.js of each one shown below, I ran into a font differentiation between them. Even I know Express is a framework for Node, I couldn't find anywhere why typography change though.
Node.js:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Express.js:
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
Output For Node.js:
Output For Express.js:
and here is Express.js version handles the same job
Well, no, not entirely. Your "plain Node" example explicitly sets the content-type to "text/plain", but you don't do the same for the Express example, in which case it will default to "text/html".
If the server tells the browser that the response contains HTML, the browser will apply a default CSS stylesheet, which usually includes a body font (something like Times New Roman).
When you use "text/plain", most browsers will render the content in a monospaced font.

Am I missing a package?

I have written the following code so far:
var http = require('http');
http.createServer(function (request,response) {
homeRoute(request, response);
}).listen(1337);
console.log('Server running at http://<Dans-Laptop>/');
function homeRoute(request, response) {
//response.writeHead(200, {'Content-Type': 'text/plain'});
//response.write('Header\n');
//response.write('Search\n');
//response.end('Footer\n');
//response.end('Hello world\n');
//if url == "/" && POST
//redirect to /:username
The problem I'm having is that as soon as I enter "function homeRoute(request, response) { and run the Server.js file from the CMD I get a SyntaxError: Unexpected token > at exports.runInThisContext etc..
If I blank out that line using // everything up until there is working fine i.e the server starts running at http:///
What is the problem with the last bit of code? Am I missing a package?
I have answered this query.. changed the port to 3000 and it appears to be fine!
Thanks.

Running Node.js Server using User Level Root

Basic question but not sure where to turn to start figuring this out.
I've setup a very simple node server on port 3000 that just responds with an index.html file. When I call http://localhost:3000 in the browser, I get the proper page served up with dependencies. I don't want to authenticate every time though so I'd like to run it from the user-level.
I tried typing http://localhost~myusername:3000 in the browser but I keep getting:
The requested URL /~myusername:3000 was not found on this server.
(I have setup user-level root to be accessed through ~/Sites and have gotten access to files through here, even php, it's just when I start using a node server this problem occurs.)
How can I get node.js to respond to user-level requests? And it serve up the proper index.html from the relative path of the user-level root instead of /library/WebServer/Documents?
Update
Code of server.js:
var http = require('http');
var fs = require('fs');
function send404(response) {
response.writeHead(404, { 'Content-Type': 'text/plain' });
response.write('Error 404: Resource not found.');
response.end();
}
var server = http.createServer(function (req, res) {
if (req.method == 'GET' && req.url == '/') {
res.writeHead(200, { 'content-type': 'text/html' });
fs.createReadStream('./index.html').pipe(res);
}
else {
send404(res);
}
}).listen(3000);
console.log('server running on port 3000');

Why can't I run this node app on port 3000?

I am experimenting with a simple node app...
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
I am able to browse to host:1337/ and I see 'Hello World'. However, if I change the port to 3000, I can't load the page.
You might have another service running at port 3000.
type
netstat -a -b
on the command prompt
it will list all listening ports.. check if 3000 is in use.
I changed the console log, but not the actual port number in the first argument of the listen method in this line...
}).listen(3000, "127.0.0.1");
...move along...nothing to see here! ;)

Categories