Node.js - update server page - javascript

After create the http server and listen to it:
var server = http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<!DOCTYPE "html">");
response.write("<html>");
response.write("<head>");
response.write("<title>Hello World</title>");
response.write("</head>");
response.write("<body>");
response.write("Hello World!");
response.write("</body>");
response.write("</html>");
response.end();
});
server.listen(1337);
now I want to update the page but don't want to exit() the server. How can I do it?

Use Nodemon, it is a utility that will monitor for any changes in your source files and automatically restart your server.
Steps:
Install nodemon globally npm install nodemon -g
Run the server nodemon app.js
This way you do not have to exit the server everytime you update the page/server logic.

As #Pavol Pitonak suggested, you should use multiple files, one for node server boot, second for index page, so when you edit your index.html file - server doesn't need restart.
server.js file
var http = require('http');
var path = require('path');
var fs = require('fs');
var server = http.createServer(function(req, res){
// get path to file we gonna send
var indexFilePath = path.join(__dirname, 'index.html');
// read file contents
fs.readFile(indexFilePath, function(err, contents){
if(err){
// for debugging
console.log(err);
} else {
// send file content
res.end(contents);
}
});
});
server.listen(1337);
index.html file
<!DOCTYPE html>
<html>
<body>
Hello world
</body>
</html>

Related

html page not loading when running javascript server

So I'm creating a game with HTML, css, and javascript, and I'm trying to incorporate template engines/ajax in this task. I made a server which if I run in cmd and then open up google chrome and type 'localhost:3000' in the address line, it is supposed to direct me to the main.html page.
However, when I type 'node server.js' in cmd, it runs properly but when I enter 'localhost:3000' in the browser it says the page does not exist. I'm not sure what went wrong. If I were to manually double click on the html file in my folder, it works, but I'm trying to get it to load by running a server.
I have three folders (img, node_modules, and pages) and 2 json packages which were created by installing express and pug. It's hard to explain my folder paths and code here, so I have a link to a folder containing my files/sub folders and it also gives a clearer view of the path in my directory: https://github.com/jessaisreal/game
It wouldn't let me upload the node_modules folder as it was too big, but I automatically got it from typing 'npm init', 'npm install express' and 'npm install pug' into the cmd line in the folder.
I'm assuming something is wrong with my server.js file or the way my folders are set up. I'm really desperate to get my program working and would appreciate any help. I have no idea why my html page isn't loading. I'm also not sure if I handled the GET request for getting specific fonts correctly.
I cut down my program as much as I could. There are several html and css files, but only included create and main here for simplicity. Again, I would appreciate any help or a push in the right direction!!
server.js:
const http = require('http');
const fs = require("fs");
const pug = require("pug");
//user pug functrion to render through the create Page
const renderMain = pug.compileFile('pages/main.pug');
const renderCreate = pug.compileFile('pages/create.pug');
//Helper function to send a 404 error
function send404(response){
response.statusCode = 404;
response.write("Unknown resource.");
response.end();
}
// Helper function to send 500 server error;
function send500(response){
response.statusCode = 500;
response.write("Server error.");
response.end();
}
// initilize the server
const server = http.createServer(function (request, response) {
//console.log(request.method+" -> "+request.url); test about the income request
// handle the get request
if(request.method === "GET"){
if(request.url === "/" || request.url === "/main"){
let data = renderHome("./pages/main.pug",{})
response.statusCode = 200;
response.end(data);
}else if(request.url === "/main.js"){
//read main.js file and send it back
fs.readFile("main.js", function(err, data){
if(err){
send500(response);
return;
}
response.statusCode = 200;
response.setHeader("Content-Type", "application/javascript");
response.write(data);
response.end();
});
}else if(request.url === "/main.css"){
//read css file
fs.readFile("main.css", function(err, data){
if(err){
send500(response);
return;
}
response.statusCode = 200;
response.setHeader("Content-Type", "text/css");
response.write(data);
response.end();
});
}else{
send404(response);
return;
}
}
});
//Server listens on port 3000
server.listen(3000);
console.log('Server running at http://127.0.0.1:3000/');
I highly suggest that you use expressjs for better organizing your project and this issue will be gone

Saving and loading data in a node.js server and passing it to an html5 file

So I'm very new to node.js and javascript, and i made a server that works great by loading up an html file on request. This html file does not contain any of it's own data, it simply sources from the internet and displays some images and text i wrote. I've decided to make the site play an audio file when it is opened. I know this is done easily with the <audio> tag in html5 but the src="" lets me take a file from the computer and place it there, of course when i open the site from another computer the file obviously isn't found and thus isn't played. I figure the audio file must be kept as a variable on the server and passed into the html file's <audio src= > tag. How do i do this? It is an .mp3(but i can get it to any other audio format) file about 30 seconds long. I just want it to play when the site is loaded from another computer(over the internet). Also how would i go about doing the same with pictures or any other data that i don't want to source from the internet but rather keep as data in my server?
var http = require('http');
var fs = require('fs');
var simpleServer = http.createServer(function(request, response){
response.writeHead(200, {"Content-Type":"text/html"});
fs.readFile('./Picture.html', null, function(error, data){
if(error){
response.writeHead(404);
} else{
response.write(data);
}
response.end();
})
});
simpleServer.listen(80, '0.0.0.0', function() {
console.log('Listening to port: ' + 80);
});
console.log("Server running...");
Short Answer
Bypassing using HTML altogether, you can also simply serve the audio file instead of Picture.html:
fs.readFile("./audiofile.mp3", function(error, data) {
if (error) {
response.writeHead(404);
} else {
response.writeHead(200, { "Content-Type": "audio/mpeg"});
response.end(data, 'utf-8');
}
});
Note:
You will have to replace the filename audiofile.mp3 and the content type audio/mpeg to their appropriate values for the file you want to send.
Check Mozilla's Complete List of MIME Types for a full list of file extensions and their associated content types.
Better Answer:
The http module is fairly low-level and is unnecessarily complicated if you're learning.
You can install express.js to your project using the command npm install express --save.
With express your code simplifies to:
const express = require('express');
const app = express();
const port = 80;
app.get('/', (request, response) => {
response.sendFile(__dirname + '/Picture.html');
});
// Anything put in the public folder is available to the world!
app.use(express.static(__dirname + '/public'));
app.listen(port, () => {
console.log(`Listening on port: ${port}`)
});
Then you just have to place all your files into a folder called "public" under your project directory and you can call them from HTML!

Node js stream file content and stop reading

I need to stream a log file to my frontend from node js backend and when the user leaves the page stops reading file, I was able to stream ok with tail-stream module and with require('child_process').spawn running tail -f command, but the backend remains running after leaving the page.
What can I do to stop the backend script that keeps watching the file?
here is an example that works as I said before.
var sys = require('sys')
var spawn = require('child_process').spawn;
var filename = process.argv[2];
if (!filename)
return sys.puts("Usage: node <server.js> <filename>");
var tail = spawn("tail", ["-f", filename]);
http = require('http');
http.createServer(function (req, res) {
sys.puts("new connection..");
res.writeHead(200, {'Content-Type': "text/plain;charset=UTF-8"});
tail.stdout.on("data", function (data) {
console.log(new Date());
res.write(data);
});
}).listen(3000);
Thanks in advance.

NodeJS Error EADDRINUSE and HTML file not rendering, instead shows html codes

I'm new kid on the block with NodeJS. Right now im following a basic tutorial of NodeJS, so far so good.
But I have a problem using fs.createReadStream method:.
var http = require("http");
var fs = require("fs");
function fourOHfour(response) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("four oh four.....");
response.end();
}
function onRequest (request, response) {
if(request.method == 'GET' && request.url == '/'){
response.writeHead(200, {"Content-Type": "text/plain"});
fs.createReadStream("./index.html").pipe(response);
}
else{
fourOHfour(response);
}
}
http.createServer(onRequest).listen(8888);
console.log("server is running......");
When I go on my browser and type localhost:8888, it should be rendering the index.html file in HTML but the result is wrong, all I get is a bunch of codes of index.html file - plain text.
Meanwhile in my sublime compiler, I've got no error in regards to this case. Until i try to edit my code, whatever I cahnge, it will give me an error like this:
If that thing happen, I cant fix the error unless I restart the laptop, then everything running well again. At least my compiler say that the server is running... Even thought my localhost:8888 still not rendering the HTML file.
You are specifying your content type as: text/plain which means the page will not render in HTML but instead, plain text. This is why you see the "codes" from your HTML file instead of the actual HTML being rendered.
To fix that problem, set the content type to text/html like so:
response.writeHeader(200, {"Content-Type": "text/html"});
In regards to the error you posted, "EADDRINUSE"
EADDRINUSE means that the port number which listen() tries to bind the server to is already in use.
So, in your case, there must be running a server on port 8888 already.
Check for the listening event like this, to see if the server is really listening:
var http=require('http');
var server=http.createServer(function(req,res){
res.end('test');
});
server.on('listening',function(){
console.log('ok, server is running');
});
server.listen(8888);
EADDRINUSE - seems like port is busy by another process or maybe by same nodejs process that don't want to close.
try to kill process:
killall node
about code - try this:
var http = require("http"),
fs = require("fs"),
URL = require('url');
function output(response, body, status) {
status = status || 200;
response.writeHead(status, {"Content-Type": "text/html"});
response.end(body);
}
function fourOHfour(response) {
output(response, 404, "four oh four...");
}
function onRequest (request, response) {
var uri = URL.parse(request.url).pathname; // extractin URI part
var method = request.method || 'GET'; // detecting method otherwise GET
if(method == 'GET' && uri == '/'){
// reading file
fs.readFile(__dirname+'/index.html', function(err, data) {
if(err) { // if error happen, output error
output(response, err, 500);
return;
}
output(response, data); // ouput html body
});
return;
}
fourOHfour(response);
}
var httpServer = http.createServer();
httpServer.on('request', onRequest);
httpServer.listen(8888);
To run Your code in production do following in terminal:
install forever:
sudo npm install -g forever # remove sudo word if You use windows, or You're already root user
start app using forever:
forever start app.js
To run Your code in development environment:
install nodemon:
sudo npm install -g nodemon # remove sudo word if You use windows, or You're already root user
run Your app using nodemon:
nodemon app.js
Forever will keep Your app running and will output logs which You can see using:
forever list # lists running processes
forever logs # shows logs that outputs forever
forever logs 0 # read logs of 0-th process
To restart forever process:
forever restartall # restarts all forever instances
forever restart 0 # restarts first process
To stop:
forever stopall
forever stop 0
About Nodemon: it's a tool that watches changes in Your file and restarts it automatically, no need to stop-start Your app, so that's why I prefer nodemon in dev environment

Node.js simplest code not working

As I am a newbie to Node.js and is learning from different articles. So, far I have learnt, my code is
At server side with app.js
var http = require('http');
var app = http.createServer(function(req,res)
{
req.on('end',function()
{
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello');
});
});
var io = require('socket.io').listen(app);
io.sockets.on('connection',function(socket)
{
socket.emit('connect',{msg:'Hello Client'});
socket.on('client_Says',console.log);
});
app.listen(3000);
At client side with index.html
<script type="text/javascript" src="//localhost:3000/socket.io/socket.io.js"></script>
<script type="text/javascript">
var socket = io.connect('//localhost:3000');
socket.on('connect',function(data)
{
alert('Server says '+data.msg);
socket.emit('client_Says',{data:'Hello Server'});
});
</script>
What is that I am doing wrong in above code? When I run app.js in console, it says info - socket.io started but when I run http://localhost:3000 it just keep requesting server.
plus I want to know that is it true that wherever on my pc I create my folder for Node and place app.js and index.html files like above in it and run http://localhost:3000 in browser will automatically make that folder my site folder for localhost after running app.js in Node console?
In your app.js update code to this
var http = require('http'),
fs = require('fs'), //<--- File Module
index = fs.readFileSync(__dirname + '/index.html');
var app = http.createServer(function(req,res)
{
res.writeHead(200, {'Content-Type': 'text/html'}); //<-Updated to text/html
res.end(index); //<---I am sending page
});
Hope that solves your problem
You're not supposed to do this on server side:
socket.emit('connect',{msg:'Hello Client'});
because connect is a default event which is emitted on a successful connection from the server. So when a client connects, the server fires its default 'connect' event, but here you're also triggering your event named connect which might be causing problem.

Categories

Resources