How to run a website developed with node.js in local? - javascript

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.

Related

Nodejs webserver use script port 3000

I created a script in nodejs that extract data from database and create a file with all db data. I create also a webserver node js listening on port 3000 and with forever is working in listening mode all time. But now the script is without web interface and use a command line prompt and other modules. How remote users can use a remote script like : node scrip.js like a command shell prompt on locale machine ?
This code working but in-globe only hello world not my prompt command and db retrieve info. Thanks
var app = connect().use(connect.static('public')).listen(3000, "0.0.0.0");
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, '127.0.0.1');
console.log('Server running at http://127.0.0.1:3000/');
I'm not completely sure I understand what you are asking. But if you are just asking how to communicate with a local server running on port 3000.
Then if this is a GET request, you could simply type 'http://localhost:3000/' in a browser, or you could use a tool like postman, I recommend the Chrome app.
Local server is listening on port 3000, but i can see only hello world. But when one user access to my url http://ip:3000/ needs to use a command shell like >
node script.js because i have different operations db retrieve information and input user data from command line input.

making http server with node and browserify

im making a mobile app that require a http server so im trying to make it with browserify so this is what im doing, i got this server.js
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 i do this
$ browserify server.js > browserify.js
then i enmbeded to my html like this
<html>
<body>
<script src="https.js"></script>
</body>
</html>
then when i run the html i get this error
Uncaught TypeError: http.createServer is not a function
how can i get a http server working on the browser ?
by the way i got http-browserify installed
Apologies if I'm reading this question wrong, but you can't create an http server from within a browser. That http.createServer call is only meant to be called (and is only defined) in node.js.
So you would need to run your script from node from the command line, like this:
node https.js

How running Node.js files works?

With my current web development practices, I always run my code (html, css, javascript) on an internet browser.
I'm trying to learn Node.js, and all the tutorials are using command line to run the node.js files.
My problem is, I don't know how to use terminal for development purposes (I've never really used it at all)
Why would I even be running a website on a terminal window rather than a browser where it should be run?
And lastly, can I run node.js code on a web browser, similar to how I run my javascript files? Or do i HAVE TO run Node.js on a terminal window if I want to test it or something?
This content shows 3 basic steps:
Step 1 Install Node.js
Step 2 Use an editor to write some JavaScript
Step 3 Run the application "node appName.js" and a browser to test "host-ip:port" ex: http://127.0.0.1:3000
source code
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');

How to run multiple Node.js application while in development

I am trying to run two node.js application in my development machine but the second application throwing the following exception while running.
Is there any way to run these two applications in parallel way ?
You need to use a different port since 3000 is already in use.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3001, "127.0.0.1");
Address is already in use, you should change the port from for example 3000 to 3001 for the second instance of the script.
You can't bind two sockets on the same port. Hence the error.
The usual good practice is to rely on the PORT environment variable so as to be able to quickly change the listening port from the command line.
var http = require('http');
var port = process.env.PORT || 3000;
http.createServer().listen(port);
Then launch your application:
$ PORT=8080 node app.js
See here for cross-platform instructions on how to define environment variables from the command line.

Node.js on a remote server: io is not defined

I'm building an application that links to a node.js hosted on my home computer (78.233.79.103:8000)
The server is properly istalled (if you go to the addres I gave you'll se the socket.io works)
If I run the server in localhost with
node server.js
all is good, the application works
Then when I run the application on my other pc or on my iPad (I wrapped it with phoneGap so it's just a web app included in a native iOS app), trying to connect the io on 78.233.79.103:8000 i got the console log:
io is not defined
here is my sourcecode: https://github.com/synbioz/puissance4
look for the server.js
I know I only call io = require('socket.io').listen(PORT); but kwnow also I should create something like:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8000, '78.233.79.103');
that actually doesn't works
Any idea?
probably because .listen() doesn't return this.
try writing them in separate lines.
var io = require('socket.io');
io.listen(PORT);

Categories

Resources