read and print file contents with nodejs server? - javascript

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.

Related

Raspberry Pi Host a html page

I am using Raspberry Pi 3 with Windows IoT 10.I want to open a html webpage in the web browser.Here raspberry pi as web server,it has to display html page. I have tried in node js,but i am unable to read the html file. Here is my
code
var http = require('http');
var fs = require('fs');
var path = require('path');
var filepath;
filepath = path.join(__dirname,'/assets/index.html');
console.log(filepath);
http.createServer(function (req, res) {
// console.log(filepath);
fs.readFile(filepath, function (err, data) {
if (error) return console.error("Error in finding the file");
res.writeHead(200, { 'Content-Type': 'text/html', 'Content-Length': data.length });
res.write(data);
});
}).listen(1337);
I have create an index.html page under assets folder. Is this the correct way to host a web page or please please help me.

Syntax error with NodeJS and HTTP

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

making js and other files available to html via node.js http server

I have a very simple web server like this:
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
fs.readFile('./index.html', 'utf-8', function (err, content) {
if (err) {
res.end('something went wrong.');
return;
}
res.end(content);
});
}).listen(8080);
console.log("Server running on port 8080.")
This renders my index.html without any issues, but if I try to reference another file in my index.html via a script tag for instance, the site just gets stuck, unable to find the file which exists in the server directory.
How can I make those files available to my index.html file?
Please keep in mind that I realize this can be done much more easily with Express but I do not wish to use Express. I am trying to learn how things work behind the scene. Thanks in advance.
You need to make the directory visible to public. Its is recommend to use framework while developing the Node.js application.
Here is the code below to server file without framework.
var basePath = __dirname;
var http = require('http');
var fs = require('fs');
var path = require('path');
http.createServer(function(req, res) {
var stream = fs.createReadStream(path.join(basePath, req.url));
stream.on('error', function() {
res.writeHead(404);
res.end();
});
stream.pipe(res);
}).listen(9999);
Refer : Node itself can serve static files without express or any other module..?

how use createwriteStream with javascript

how i can send file for that the server read it with fs.createwriteStream();
var http = require('http');
var fs = require('fs');
http.createServer(function(req, res) {
// This opens up the writeable stream to `output`
var writeStream = fs.createWriteStream('./output');
// This pipes the POST data to the file
req.pipe(writeStream);
req.on('end', function () {
res.end('ok upload file');
});
// This is here incase any errors occur
writeStream.on('error', function (err) {
console.log(err);
});
}).listen(8080);
i use this command with console linux:
curl --upload-file hello.txt 127.0.0.1:3000 and work ok but i don't know how send file with url
somebody know?
curl -i -F name=test -F filedata=#localfile.jpg 127.0.0.1:3000

Executing javascript code with node.js

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

Categories