Does Chrome automatically recognize node.js - javascript

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.

Related

browserify Error : http.createServer is not a function

I tried to browserify this node js script :
var phantom = require('phantom')
phantom.create(function(ph) {
ph.createPage(function(page) {
page.open("editor.html", function(status) {
console.log("opened diagram? ", status);
page.evaluate(function() {
return document.getElementById("GraphImage").src;
}, function(result) {
//console.log(result);
ph.exit();
});
});
});
});
So I used this command:
browserify myscript.js > bundle.js
and when I run bundle.js from an html file I get this error:
http.createServer is not a function
it seems that browserify does not support httpserver. How can I resolve this problem?
You can't run a web server from inside a web browser. There really isn't anything in the browser that could act like Node's http module. Also it doesn't make sense to run PhantomJS in a browser, because PhantomJS is a web browser.
What is the desired behavior you are trying to accomplish?
Update:
It seems like you are trying to run code intended for Node.js inside a browser instead.
The JavaScript engine inside the browser is much more restrictive than in Node.js, for example you can't access the file system from inside the browser for security reasons (or else you could read the hard drive of anyone who visited your web page).
Browserify does include some "shims" that will put small JS libraries into your code that work in the browser and match the API of Node.js, allowing some Node.js specific JS code to execute in the browser.
In your case, you are requiring Phantom, which seems to in turn require http. Accoring to the Browserify documentation, it will see require('http') and include a shim for the http module (because browser's don't provide an http module of their own).
The Phantom module then tries to call http.createServer() but accoring to the documentation for that http shim:
This module implements http.request, http.get, and most of http.ClientRequest and http.IncomingMessage in addition to http.METHODS and http.STATUS_CODES.
so http.createServer() is not supported by the shim. This also makes sense because a browser would never let you open an http server inside of itself, or else navigation to someone's web site could cause your browser to start serving content to the outside world, which obviously doesn't make sense.
In your comment:
"i want that my node js script can be executed from another JS code"
You don't specify what "other JS code" is running in. If that JS code is already running in Node, then you don't need Browserify at all. If you are trying to have a web browser start up an actual Node.js process, that isn't going to happen, again for obvious security reasons, because browsing to a web page shouldn't have the ability to run any executable on your system.
What Browserify lets you do is take code originally intended for Node.js, and run it in a browser instead, but a t runtime it is executing in the browser, not in Node.js, so you can only use JS code that works within the constraints of the browser's JS runtime.
If you are trying to execute your code in Node.js, then you need to do that by having something start the Node.js executable, either from the command line or by having another program start the process for you, but that can't be done from within a web browser. If you are trying to have users navigate to your web site and then have this code run on their machines in a browser and not in Node.js, then you need to only use modules that work in the browser.

Node.js closes as I open it

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.)

How to run server written in js with Node.js

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!

Where do I need to save javascript files for Node.js in windows 8

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.

Starting a node.js server

I recently got into node and I installed it on my localhost. I am using WAMP. I am on Windows Vista.
Anwyay, I installed it. I made a new file in my localhost directory with this being called server.js
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*'
});
response.end('Hello World\n');
}).listen(1337);
then I went to node and tried typing % node server.js and all I got was an ellipses. What gives?
UPDATE: I checked my Systems variable and saw that my PATH lists the node.js as C:\Program Files (x86)\nodejs\
Run cmd and then run node server.js. In your example, you are trying to use the REPL to run your command, which is not going to work. The ellipsis is node.js expecting more tokens before closing the current scope (you can type code in and run it on the fly here)
The NodeJS code that you wrote is corrected, and it should work but as #dwerner said you have to write node server.js in the command prompt not inside the Node REPL.
But today most of who work with NodeJS are developing using a development environment (IDE). By using IDE you get a hotkey for running your code, and many things that can help you in the daily development (Syntax highlight for e.g.)
One of the most popular IDE's today for NodeJS development is VSCode, you can check it out.
As dwerner and aminadav mentioned, you need to run the node command for the main .js file you're using for your script/app. This file will typically be index.js by default, when you run npm init to create the package.json for your NodeJS project.
Maybe you will find this blog post that covers the basics helpful as well. :)
https://dev.to/bishopwm/my-first-server-and-rest-api-essentials-for-frontenders-2gnk

Categories