I am a very beginner at node js programming language. I just want to asked that why i have to run on command line after each change in js file.
for example:
i changed form this
var http = require('http');
http.createServer(function (req, res) {
res.write("The date and time are currently displaying");
res.end('Hello World!');
}).listen(9001);
to
var http = require('http');
http.createServer(function (req, res) {
res.write("The date");
res.end('Hello World!');
}).listen(9001);
and when i refresh it did not change in browser.
And when i run it through cmd and then refresh the browser it changed.
Kindly tell me what should i do to instant change in browser whenever i
change in js file.And why it does't changed.
You might be running your node server using
node filename.js
If you want to just restart the server after every change made, you should use the nodemon package of npm.
Do:
Step1: Install nodemon using
(sudo) npm install -g nodemon
Step 2: After installation, just go to project folder and run
nodemon
Now on any change made to the node file, the node server will restart automatically with the latest changes.
Related
I'm so new in node.js and I have a simple node.js project with only one js file (vpn.js) which use a module and an index.html which opens using a function in vpn.js. I have installed this package and the require function can find its module. I have a vpn.js file and an index.html (in index.html I only have a video tag with a src.). Now my question is should I always run my code with terminal? how should I host this project? Basically no clients can run terminal commands on web. (note: I'm using Windows not Linux)
this is the code of my js file:
const openvpnmanager = require('node-openvpn');
const opts = {
host: '192.168.1.7', // normally '127.0.0.1', will default to if undefined
port: 8080, //port openvpn management console
timeout: 1500, //timeout for connection - optional, will default to 1500ms if undefined
logpath: 'log.txt' //optional write openvpn console output to file, can be relative path or absolute
};
const auth = {
user: 'vpnUserName',
pass: 'vpnPassword',
};
const openvpn = openvpnmanager.connect(opts)
// will be emited on successful interfacing with openvpn instance
openvpn.on('connected', () => {
//openvpnmanager.authorize(auth);
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('index.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(5500);
});
// emits console output of openvpn instance as a string
openvpn.on('console-output', output => {
console.log(output);
});
// emits console output of openvpn state as a array
openvpn.on('state-change', state => {
console.log(state)
});
// emits console output of openvpn state as a string
openvpn.on('error', error => {
console.log(error)
});
Use pkg npm package. This will create an executable file for your nodejs project. You can create executable file for Windows or mac or linux.
Install pkg globally using following command
npm install -g pkg
After installing it, use:
pkg app.js[entry file to your project] to create executable file.
For more info about pkg, look into pkg
you can done it help of set autorun the node server forever.
here is some step
You may also want to consider using the upstart utility. It will allow you to start, stop and restart you node application like a service. Upstart can configured to automatically restart your application if it crashes.
Install upstart:
sudo apt-get install upstart
Create a simple script for your application that will look something like:
#!upstart
description "my app"
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
env NODE_ENV=production
exec node /somepath/myapp/app.js >> /var/log/myapp.log 2>&1
Then copy the script file (myapp.conf) to /etc/init and make sure its marked as executable. Your application can then be managed using the following commands:
sudo start myapp
sudo stop myapp
sudo restart myapp
I have tried opening my server.js file multiple times as well as a test file i named helloworld.js in cmd as well as gitbash and gitcmd. Everytime I am returned with some sort of error whether it is a syntax:
or a cannot find module socket.io error
I have tried many things and ways including downloading gitbash and trying it there. I am fully new to any coding or dev and have read all of the other 20+ psots regarding the same thing but none of them seemed to have been resolved or documented?
Thank you
In your first attempt:
node server.js is a command you are expected to run on your shell (bash, Windows Power Shell, etc). It launches Node.js and tells it to run the server.js module.
You are running node at the shell, which launches node and presents you with a REPL. You are then trying to run node server.js as if it were JavaScript (which it isn't, because it is shell).
Where you are currently typing node, instead type node server.js.
In your second attempt:
You are trying to load a module called socket.io, but it is not installed. You have to install it first.
Look at the getting started guide for Socket.io.
It tells you how to set up a package manifest:
First let’s create a package.json manifest file that describes our
project. I recommend you place it in a dedicated empty directory (I’ll
call mine chat-example).
{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {}
}
and install modules while recording them as dependancies for your package:
During development, socket.io serves the client automatically for us,
as we’ll see, so for now we only have to install one module:
npm install --save socket.io
If you have not already, go through the Getting Started guide on the Socket.io website. From the guide:
Make sure you have Node along with npm installed
Perform an npm init to initialize your project
perform an npm install --save express
perform npm install --save socket.io
When you want to run your Node app, do node server.js
Here is an example of what a correctly-formatted Socket.io server file should look like:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
Make sure you have this in your client HTML file:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
I am new to node.js and heroku.
By following docs and some tutorials I have successfully deployed my app on heroku.
The problem I am facing is that I don't know how to run that deployed app on heroku. After reading a number of tutorials, its still not clear to me.
When I run the commands
$ git add .
$ git commit -am "make it better"
$ git push heroku master
from local command line, it executes the scripts. But when I click on the open app from the heroku account, it does not execute all of files.
I am running 1 web dynos and heroku shows that my web dyno executes following command
web node build/server.js
The server.js file contains following code
require('./routes/main.routes.js');
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("this is a test page");
}).listen(process.env.PORT);
when I click on open app from heroku, it opens the app in the browser with message
this is a test page
It does not run the main.routes.js file.
But when I click on restart all Dynos, the main.routes.js file also gets executed. I want the app to execute the main.routes.js file when I click on open my app or refresh it in the browser window.
Is the restart all Dynos only way to execute script? or I have done something wrong?
Any help will be much appreciated.
You can try this replace the comment (//write the code here) and try requesting the url:
var http = require('http');
http.createServer(function (req, res) {
// write the code here if it needs to execute every time
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("this is a test page");
}).listen(process.env.PORT);
I would like to run a website developed with node.js in local.
I already installed node.js but when I lauch a .js file on my terminal, nothing happen ( $ node file.js )
Also, I guess I have to simulate a server ? How can I do that with node?
You can start a simple server with the example that can be found on nodejs.org:
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/');
https://nodejs.org/en/about/
To develop a website it is very helpful to use a web framework such as Express.
http://expressjs.com/
You should use:
npm start file.js
but also be sure to check out nodemon, which is very helpful for debugging - it restarts your app on code change.
Also be sure to check out the express generator, which will set up a node+express app that you can check out to figure how to get the server and routes going.
I launched and amazon ec2 instance running ubuntu 12.04
I then followed the instructions to install node.js from here http://howtonode.org/how-to-install-nodejs
sudo apt-get install g++ curl libssl-dev apache2-utils
sudo apt-get install git-core
git clone git://github.com/ry/node.git
cd node
./configure
make
sudo make install
I then used the example code to make hello.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.js\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
I then ran hello.js
/var/www$ node hello.js
Server running at http://127.0.0.1:8124/
However, when I try to access this from the url, with http://ec2-***.compute-1.amazonaws.com:8124/ I get an error page from my browser.
Any advice on how to get it to show up in the browser?
EDIT
I'm still encountering this problem after changing the above line of code
}).listen(8124, "127.0.0.1");
to this
}).listen(8124);
127.0.0.1 is the loopback address. Only the host can access it. If you want to listen on any available IP, just don't specify that parameter. Otherwise, specify the real IP you want to listen on.
I also had same issue, Uninstalling skype worked for me, You can also search for some setting which force skype to move to some other port.