I am running a very simple script in nodejs, loading a static html file.
var http = require("http");
fs = require('fs');
fs.readFile('./index.html', function (err, html) {
if (err) {
throw err;
}
});
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(1994);
Whenever I launch the application it immediately closes. I see the text on the console for the half second that the application launches, but then the window closes. This happens every time.
You are most probably using the Node.js repl which comes with any installation of Node.js.
I get no errors when I run your code (assuming I have index.html in my directory and no error is thrown).
Try to run your code via the command line with the commandnode yourscriptfilename.js
After uninstalling and re-installing nodejs, and then updating the problem was solved. I think that it was simply node being new to windows with a few unaccounted for issues. The problem was a few months ago, so I forget the exact specifics. Using command prompt shells in windows leads to problems (as one can imagine.)
Related
I'm learning nodejs and I want to send some file to the printing queue.
I tried elctron-printer and node-printer modules but actually they do not work proprly (can't detect printer with printer.list command for example). Now I'm trying to make it with child_process module and I want to know is there any posibility to start file with its associated application with "print" argument like a python can do it?
For example, this is a code sample of file execution with nodejs:
var childProcess = require('child_process');
childProcess.exec('start printme.txt', function (err, stdout, stderr) {
if (err) {
console.error(err);
return;
}
console.log(stdout);
process.exit(0);// exit process once it is opened
})
Unfortunately it seems that "print" argument is invalid for this code.
And this is a code sample for python and it works fine on windows:
import os
os.startfile('printme.txt', 'print')
All in all I hope that there is possibility to emulate system commands with nodejs.
Otherwise I will have to execute python script via nodejs just for file printing, something like this:
let python = spawn('python', [path.join(app.getAppPath(), '..', 'python_scripts/print_file.py'])
But it is terrible way to do it.
If I create a Javascript file called server.js containing just the following code:
var http = require("http");
http.createServer(function (request, response) {
//debugger;
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
and have no other files in the directory (no html or other javascript code) and then run the following command:
devtool server.js --watch
the Chrome debugger window launches. I can then put a breakpoint within the code. Then in my browser, if I navigate to localhost:8888, my breakpoint will hit.
I have node.js installed. My question is how Chrome even knows what the require("http") even is as I have no dependencies nor am I referencing the requirejs library. So how is it that Chrome is able to run this node.js code? Does Chrome somehow know that node.js is installed?
That is because you are using the devtool tool, which must be this devtool ?
It communicates the code to the browser, for you to debug it, and translates it into browser executable code using browserify.
devtool knows that it is some node.js code, Chrome does not.
I'm pretty new to node and I'm trying to create a simple server for my website but when I type in node server.js in command prompt nothing happens.
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("It's alive!");
response.end();
}).listen(3000);
This is what I see when I try to run my server.js file:
I'm fairly certain my code is right, is there anything else I'm doing wrong?
The server is working just fine. You need to visit http://localhost:3000/ from your browser to view the expected output ("It's alive!").
To write messages to the console, use console.log().
The console output you show seems correct given your code.
Did you open a webbrowser and try opening http://localhost:3000?
If you want to see some console output to confirm your server started up, try adding this at the end of your server.js file:
console.log('Server running at http://localhost:3000');
You only need to hit the URL http://localhost:3000.
The server is already started after you hit the command "node server.js" and you will get the output - "It's alive!" because of this line present in your code: response.write("It's alive!");
First time starting up node server made a very small mistake of not calling "response.end" as a function that made the server not response and take very long to load. Should have been response.end()
const server = http.createServer(function(request, response){
response.writeHead(200,{'Content-Type': 'text/html'})
response.write('Hello Node')
response.end
})
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!
I have JUST downloaded node.js and am having to work with their command line for the first time. It appears that every tutorial on the planet gives the same starter app.
It uses the code...
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
and wants me to save it to the file example.js.
The file can then be run by typing...
node example.js
but all the command line gives me is
...
The tutorials do not say WHERE I should save the file. To my C drive? To the same file as node.js? Anywhere?
I have tried all three and they don't seem to work. If there is any other solution, or simply something else I should be looking into and asking about, that help would be appreciated too. But at this point I honestly have no idea what the problem is, and there appears to be very few resources to help me here.
When you run node example.js from your prompt, it's assuming that example.js is in the current working directory.
To change your current working directory, use the change directory (cd) command:
cd C:\Projects\MyProject
node example.js
This is equivalent to
node C:\Projects\MyProject\example.js
Correct! you can save it any where and change to that directory by the (cd) command on node.js command prompt which will then give message says: "Server running at: 127.0.0.1:8124.
So point your browser to that address and you see your file.