node.exe does not read file, but returns ellipsis (...) - node.js

End feb 2014 I downloaded node to c:\dev\0.10\ of my windows 7 machine, and node.exe opens fine.
Inspired by Smashing Node (some book), I want to achieve the following:
Firefox shows the plain text Smashing Node! if i point it to localhost:3000 and run in the node console
node my-web-server.js
where my-web-server.js file next to node.exe contains
require('http').createServer(function(req,res){res.writeHead(200, ('Content-Type', 'text/html'));res.end('<marquee>Smashing Node!</marquee>');}).listen(3000);;
but I fail: browser says
cannot connect with webserver on localhost:3000.
If paste the above oneliner in node it reacts with
{ domain: null,
_events: ..etc... }
Ignoring that, browser F5 results in Smashing Node!.
Node refuses the simplest of files, say have a file called hello.js next to node.exe and file contains the ascii text
console.log("hello");
i type:
node hello.js
node returns
...
(an ellipsis in dark gray)
Expected was: node returns hello
i type a file that does not exist like this:
node die
node returns
...
if i type
var http = require('http');
node:
undefined
(in a darkish gray) Expected was: something like ok, especially since the above oneliner resulted in a web server.
If however i type
npm install colors
node reacts with
npm should be run outside of the node repl, in your normal shell. (Press Control-D to exit.)
of course
node --version
also responds with an ellipsis.
What can i do?
How do i make node to process files?

You need to run node hello.js (and npm) on your command line shell (e.g. cmd.com or Windows PowerShell).
You are trying to run it in the Node REPL (node console), where you are expected to type only JavaScript.

It's hard to tell where the Smashing Node failed, particularly as a one-liner. The code runs OK on my machine, but you can split the code as follow and add a few console.log() to see how is executing:
console.log('about to start listening for requests');
require('http').createServer( function(req,res) {
console.log('a request was received', req.url);
res.writeHead(200, ('Content-Type', 'text/html'));
res.end('<marquee>Smashing Node!</marquee>');
}).listen(3000);;
console.log('listening for requests');
Save this code as my-web-server.js as you did before and then run from the command line "node my-web-server.js" and point your browser to localhost:3000
Also, I've got a very basic Node.js tutorial that might help you with the basics: http://hectorcorrea.com/#/blog/introduction-to-node-js/51

Related

Need help getting simple node.js express to run

I'm trying a simple sample of node express that I copied from online. The script is below which I think is pretty standard.
var http = require('http');
http.createServer(function(request, response)){
response.writeHead(200);
response.write("hello");
response.end();
}).listen(8080);
console.log('listening on port 8080...');
I used the bash on ubuntu on windows command as follows:
npm init
node SampleServer.js (the name of my file)
When I do this, I expect some response from the command line. But when I enter the "node SamplerServer.js" command, nothing happens. When I direct the browser to port 8080, I get an error message as well.
I'm using nodeclipse and the installing that on my machine was pretty complicated. Prior to any of the steps above, I created an express project in eclipse ide. It seems to perform a lot of pre steps but in the end, I think I'm getting some of the error messages below. I'm mentioning this because I'm thinking perhaps I installed one of the modules wrong.
enter image description here
Start with simple stuff...
Use express-generator to create simple app by the following command
1-> npm install -g express-generator with root or Admin access
2-> Run express demoapp.
3-> Navigate to demoapp
4-> Do npm install
5-> Run from command with npm start: it run by default on http://localhost:3000
Hit that URL from Browser

How to install node.js and create project in Eclipse

The steps I've tried:
1.(OK) install node from official website: https://nodejs.org/en/download/
Result: I'm able to open cmd(in any location, type node then use commands like "console.log" and it prints my messages)
2.(Failure) install express using npm install -g express from cmd gives me an error(picture attached
3.(OK) I've succeed installing express using the following command npm install express (without -g)
4.(OK) Writing a simple Hello World program works. Javascript file:
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");
5.(Failure) However, I wanna run a bigger project, where besides one js file, I also have an index.html file. If I move both files to node installation directory, everything works. But I wanna be able to keep my projects somewhere else. If I try to run with node C:\Users\marius\Downloads\chat-example-master\indes.js I get the error: Cannot find module express. Thus it seems that when I installed express without "-g" I got it working only in node directory.(let me know if you have any doubt).
6.(Failure) When creating a Node.js project from Eclipse, I choose empty project, no template, then add a single and simple js file(the one with Hello World), right click on project name -> run as -> run Configuration -> Node Application -> New -> add my .js file -> Run. I get the following error:
Exception occurred executing command line.(steps from http://techprd.com/how-to-setup-node-js-project-in-eclipse/)
Cannot run program "node" (in directory "C:\Users\marius\workspace\FirstNodeProject"): CreateProcess error=2, The system cannot find the file specified
To recap: What I want is to be able to run node projects located anywhere with "node" in cmd and to create node.js and express project and run them from Eclipse.
Please let me know if you need more information.
Just to let others know if they come across this issue. I can run express apps from anywhere but in the root folder of every app I have to npm install express.
In Eclipse all you need to do is: Window->Preferences->Nodeclipse->uncheck "find .Node on PATH" and insert into Node.js path input your node.exe location (in my case: C:\Program Files\nodejs\node.exe)

node command vs app.listen()

I'm confused. So if I use gulp-develop-server, it's got a app.listen():
gulpfile.js
config.server.path is set to './app,js'
server = require('gulp-develop-server');
gulp.task('default', ['server:start'], function(){
});
gulp.task('server:start', function() {
server.listen({ path: config.server.path});
});
My app.js has this:
var koa = require('koa')();
koa.listen(config.server.port, function(){
console.log('Koa app is listening on port ' + config.server.port);
});
so I'm trying to understand better how node is being started. I see people mention doing it manually like "node app.js". So doesn't koa.listen() automatically do a "node" command to start the koa web server? If I use gulp-develop-server and specify server.listen, isn't that doing 2 server.listen() for node?
Just trying to understand the basics here and can't understand why anyone would manually type in 'node [file with .listen]' manually. I'm not doing that manually and server.listen() obviously uses the 'node' command on my app.js.
Your gulpfile is a Node script. So when you run gulp server:start you're executing a Node application, the gulp command is essentially node plus some extra functionality.
The way gulp-develop-server works is it runs an additional Node application as a child process. server.listen is basically just telling gulp-develop-server what script to run.
The naming is a little confusing, but essentially what's going on is: You have 2 Node applications running on your machine (one that you can see, and one in the background), but only 1 server.

Node.js just returns "..." in terminal

I'm a front end dev trying to learn and get into Node.js
Having trouble at the first hurdle. I have an app.js file in the root of Mac: "/"
app.js has the following:
console.log('hello world!');
Whenever I run node app.js in the terminal I get the following:
> node app.js
...
It looks like you're running the Node.js REPL first. Don't do that... the Node.js REPL is for running immediate evaluations.
Just try running node app.js from your normal system terminal.

Flow of Work Working With Node.JS HTTP server

Learning Node.JS at the moment.
Everything is going fine, just that i have a little challenge with the flow of work.
So i create an HTTP server that listens at a particular port. For example
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
It works fine. Only problem is that when i edit the file that has the above code, and try to start the node process again by typing node server.js i get the following error:
Error: EADDRINUSE, Address already in use.
So i learnt I need to kill the node process using ps before the changes can be reflected and before i can restart.
But this looks like a pain. Do i need to kill the node process anytime i make changes to the server i am writing?
I am sure there is a better way. Any help?
During development I tend to just run node from the command line in a terminal window. When I'm finished with the testing I just Ctrl-C to interrupt the current process which kills node and then press arrow-up enter to restart the process.
my solution is as simple as
npm install dev -g
node-dev app.js
node-dev is the same as 'node' but automatically reruns app.js everytime any file in application dir (or subdir) is changed. it means restarting when static files are changed, too, but should be acceptable for development mode
There isn't any easy way. Authors of node do not like hot-reloading idea, so this is the way it works.
You can hack it if you put your server in a module, require it from the main script, fs.watchFile the .js for changes and then manually stop it as a reaction to a file change, manually deleting it from the module cache (require.cache if I am not mistaken), and require it again.
Or run it in child process and kill and respawn it after a file change (that is, doing automatically what you now do by hand).
You can use something like nodemon (video tutorial about it here) or node-supervisor so that the server auto-restarts when editing files.
If you want to manually do this, then just interrupt the process (CTRL+C) and re-run last command (node server.js).

Resources