Iam new to Node JS when i make any changes to the node file it is not updating in the server and also when i change the port number and kill the server and start it again it is not working
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello');
}).listen(8080);
I have found a quick solution to fix this thing. You have to install a node module called as nodemon. This module helps to automatically restart your Server once you made the changes in Source code.
npm install -g nodemon
And nodemon will be installed globally to your system path.
You can also install nodemon as a development dependency:
npm install --save-dev nodemon
By default, node does not automatically restart when there are any changes in the file.
You might want to try the package nodemon, which can watch for file changes and auto restart the server.
You can try adding an host name to your listen function
let 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');
Related
So I have a folder with a file server.js, inside server.js there is a code like this:
var http = require ('http');
http.createServer(function(req, res){
console.log(req);
console.log("***********************************");
res.writeHead(200, {'Content-Type' : 'text/html'});
res.write('Hello World');
res.end();
}).listen(80);
also in a folder with this file, I installed node modules and npm and package.json and package-lock.json
but for some reason when I try to run it with node server.json, in doesn't run on port 80 but instead this happens. https://imgur.com/KIgHhBm
How can I fix this? I want to be able to open server.js via chrome, and it should write out hello world
I've been experimenting with Node.js on my windows machine and also my linux machine to create a web server using node.
If I try my code on windows and activate my server with "node server.js" and navigate to localhost:8080 I am met with "Hello World!" as intended.
But if I try on my linux box with the same code it shows me the directory instead of serving the page. Not sure what's going on here!
Here's the code I've been using
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');
I've tried using my local ip to the machine and also using the domain followed by ":8080", both give me the same results.
I cant find anybody else with the same problem and I've been looking for hours.
I got the same issue while manually starting server from windows machine.
using command http-server -p 4200
Say, I was trying to start the server from D:/ProjectName. When I changed that and started from D:/ProjectName/build, it was working fine. Build folder has the index file and all other files.
I done everything like say on nodejs.org tutorial for helloworld but I have a big problem. Please see the image below:
IMAGE: http://i.imgur.com/EjAMXcS.png
I save example.js file into D: directory ...
How to start node.js code:
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');
You run node example.js from your command line (e.g. Windows Power Shell), not from inside the Node.js REPL.
From the command prompt navigate to D drive and then type node example.js to run your code.
When you use some package like morgan you need to install them before using them.Standard way of installing package is(again from command prompt) to navigate to your application folder and type npm install <package-name>. For further information look here:https://www.npmjs.org/doc/cli/npm-install.html
I run xampp and I was recently told by a friend to try node.js as a replacement for PHP so I thought I should give it a try
I went to node.js's website and installed the .msi file, later I put the following code in a .js file
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 launched xampp and executed "node app.js" from the command prompt, tried visiting localhost:1337 from my browser but it cannot resolve
What am I doing wrong?
I have installed node.js from here http://nodejs.org/ . in my windows8 machine. copied the example server code in my server.js file
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/');
then opened the node.js prompt and written node c:/node/server.js
but nothing happens.
I am a php developer just trying hands on it, any guidelines will really be helpful.
You don't need to go in node.js prompt, you just need to use standard command promt and write
node c:/node/server.js
this also works:
node c:\node\server.js
and then in your browser:
http://localhost:1337
Nodejs is a scripting language (like Python or Ruby, and unlike PHP or C++). To run your code, you need to enter a command in the terminal / shell / command prompt. Look for an application shortcut in your operating system by one of those names.
The command to run in the terminal will be
node server.js
But you will first need to browse in the terminal to the same folder as the file server.js. The syntax for using the terminal varies by operating system, look for its documentation.
I open a text editor, in my case I used Atom. Paste this code
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/');
and save as
helloworld.js
in
c:\xampp\htdocs\myproject
directory.
Next I open node.js commamd prompt enter
cd c:\xampp\htdocs\myproject
next
node helloworld.js
next I open my chrome browser and I type
http://localhost:1337
and there it is.
Just go on that directory of your JS file from cmd and write node jsFile.js or even node jsFile; both will work fine.
Just try
node server
from cmd prompt in that directory
If you are in a Linux container, such as on a Chromebook, you will need to manually browse to your localhost's address. I am aware the newer Chrome OS versions no longer have this problem, but on my Chromebook, I still had to manually browse to the localhost's address for your code to work.
To browse to your locahost's address, type this in command line:
sudo ifconfig
and note the inet address under eth0.
Otherwise, as others have noted, simply type node.js filename and it will work as long as you point the browser to the proper address.
Hope this helps!