Syntax error with NodeJS and HTTP - javascript

var http = require("http");
http.createServer(function(request,response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.writeHead(" This is just a start.. Remember what u thought of it.");
reponse.end();
}).listen(8888);
This is my JS code in a file named server.js which I wanted to execute through Node.js, but it's giving an error:
SyntaxError: Unexpected identifier
The path of the file, server.js is in D Drive, same as where I installed Node.js. What should I do ?

Use this code:
var http = require("http");
http.createServer(function(request,response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(" This is just a start.. Remember what u thought of it.");
response.end();
}).listen(8888);
Save it in a file named "server.js" and run it with:
node server.js

I don't know if it's a typo in your question but you use reponse instead of response:
var http = require("http");
http.createServer(function(request,response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.writeHead(" This is just a start.. Remember what u thought of it.");
reponse.end(); // <------
}).listen(8888);

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.

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');

Node.js webserver CHUNKED encoding issue

I'm trying to build a node.js webserver that listens on port 9000 and reads a js file when requested then sends the output on the response. This is what I have so far:
var http = require('http'),
fs = require('fs'),
path = require('path');
http.createServer(function(request, response){
response.writeHead(200, {'Content-Type': 'text/javascript'});
switch (request.url){
case '/main.js' :
fs.createReadStream(path.normalize('C:\\dev\\projects\\main.js')).pipe(response);
response.end()
break;
case '/page.js' :
fs.createReadStream(path.normalize('C:\\dev\\projects\\src\\page.js')).pipe(response);
response.end()
break;
case '/page2.js' :
fs.createReadStream(path.normalize('C:\\dev\\projects\\src\\page2.js')).pipe(response);
response.end()
break;
default :
response.writeHead(404);
response.write('Not Supported');
response.end();
break;
}
}).listen(9000);
console.log('listening on port 9000........')
I call the main.js file from the HTML page like so:
<script type="text/javascript" src="http://localhost:9000/main.js"></script>
When running the page, the first case in the code should run but all I get is this error in the browser console.
GET http://localhost:9000/main.js net::ERR_INVALID_CHUNKED_ENCODING
I tried playing around with the headers in the response with no success. Any ideas?

Node.js exports module not working

I am very beginner to node.js, I found a tutorial to learn. while I learn I used this code to run my server it works fine:
code :
var http = require ("http");
http.createServer (function (request, response) {
response.writeHead(200, {"Content-Type" : "text/plain"});
response.write("Hellow world! I written my first server side code!"); //browser responding
response.end();
} ).listen(8888);
But while I tried to export the server like this,
var http = require("http");
function start() {
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start; // the export module not work?
and i am getting an error in browser like this:
Failed to load resource: Could not connect to the server.
I am not able to start my server, and export the module any one suggest me the fix for this please?

Categories