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
Related
Anyone here have experience using Selenium and webdriverjs? I'm coming from a non-Java background with a good deal of experience with Node.js and JavaScript in general. According to the Selenium docs, you have to set-up a stand-alone Selenium server to use the node web driver. Fortunately, they seem to be bundled together.
npm install webdriverjs
gets you the JAR file for the standalone selenium server inside the node_modules/webdriverjs/bin directory. Example tests are inside the node node_modules/webdriverjs/examples directory but the tests in them fail when I run them from either the webdriverjs or examples directories.
What's the missing piece here? What's the quickest way to get up and running?
I have read the docs.
Note: Stack overflow wouldn't let me use the tag webdriverjs, but this is specifically about webdriverjs, not using selenium with Java or other languages.
Update: The only problem was that the built-in example tests are broken!
Here's what I did to get webdriverjs working:
Step 1: start selenium standalone in my laptop by running command java -jar selenium-server-standalone-2.33.0.jar. then it will listen to http://localhost:4444/ and you can access it via http://localhost:4444/wd/hub/. You also need to make sure Firefox browser is installed on your laptop.
Step 2: create a new directory and run command npm install webdriverjs.
Step 3: create a new file named test_webdriverjs.js in the new directory you created, and it looks like this:
var webdriverjs = require('webdriverjs');
var client = webdriverjs.remote({
host: 'localhost',
port: 4444
});
client.init();
client.url('https://github.com/')
.getTitle(function(err, title) { console.log (title)}).call(function () {});
client.end();
Then run command node test_webdriverjs.js under the same directory and you will find it works. If it doesn't work, paste out the console output.
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.
I'm using this code to make an AJAX request:
$("#userBarSignup").click(function(){
$.get("C:/xampp/htdocs/webname/resources/templates/signup.php",
{/*params*/},
function(response){
$("#signup").html("TEST");
$("#signup").html(response);
},
"html");
But from the Google Chrome JavaScript console I keep receiving this error:
XMLHttpRequest cannot load
file:///C:/xampp/htdocs/webname/resources/templates/signup.php. Cross
origin requests are only supported for HTTP.
The problem is that the signup.php file is hosted on my local web server that's where all the website is run from so it's not cross-domain.
How can I solve this problem?
I've had luck starting chrome with the following switch:
--allow-file-access-from-files
On os x try (re-type the dashes if you copy paste):
open -a 'Google Chrome' --args -allow-file-access-from-files
On other *nix run (not tested)
google-chrome --allow-file-access-from-files
or on windows edit the properties of the chrome shortcut and add the switch, e.g.
C:\ ... \Application\chrome.exe --allow-file-access-from-files
to the end of the "target" path
If you’re working on a little front-end project and want to test it locally, you’d typically open it by pointing your local directory in the web browser, for instance entering file:///home/erick/mysuperproject/index.html in your URL bar. However, if your site is trying to load resources, even if they’re placed in your local directory, you might see warnings like this:
XMLHttpRequest cannot load file:///home/erick/mysuperproject/mylibrary.js. Cross origin requests are only supported for HTTP.
Chrome and other modern browsers have implemented security restrictions for Cross Origin Requests, which means that you cannot load anything through file:/// , you need to use http:// protocol at all times, even locally -due Same Origin policies. Simple as that, you’d need to mount a webserver to run your project there.
This is not the end of the world and there are many solutions out there, including the good old Apache (with VirtualHosts if you’re running several other projects), node.js with express, a Ruby server, etc. or simply modifying your browser settings.
However there’s a simpler and lightweight solution for the lazy ones. You can use Python’s SimpleHTTPServer. It comes already bundled with python so you don’t need to install or configure anything at all!
So cd to your project directory, for instance
1
cd /home/erick/mysuperproject
and then simply use
1
python -m SimpleHTTPServer
And that’s it, you’ll see this message in your terminal
1
Serving HTTP on 0.0.0.0 port 8000 ...
So now you can go back to your browser and visit http://0.0.0.0:8000 with all your directory files served there. You can configure the port and other things, just see the documentation. But this simply trick works for me when I’m in a rush to test a new library or work out a new idea.
EDIT:
In Python 3+, SimpleHTTPServer has been replaced with http.server. So In Python 3.3, for example, the following command is equivalent:
python -m http.server 8000
You need to actually run a webserver, and make the get request to a URI on that server, rather than making the get request to a file; e.g. change the line:
$.get("C:/xampp/htdocs/webname/resources/templates/signup.php",
to read something like:
$.get("http://localhost/resources/templates/signup.php",
and the initial request page needs to be made over http as well.
I was getting the same error while trying to load simply HTML files that used JSON data to populate the page, so I used used node.js and express to solve the problem. If you do not have node installed, you need to install node first.
Install express
npm install express
Create a server.js file in the root folder of your project, in my case one folder above the files I wanted to server
Put something like the following in the server.js file and read about this on the express gihub site:
var express = require('express');
var app = express();
var path = require('path');
// __dirname will use the current path from where you run this file
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, '/FOLDERTOHTMLFILESTOSERVER')));
app.listen(8000);
console.log('Listening on port 8000');
After you've saved server.js, you can run the server using:
node server.js
Go to http://localhost:8000/FILENAME and you should see the HTML file you were trying to load
If you have nodejs installed, you can download and install the server using command line:
npm install -g http-server
Change directories to the directory where you want to serve files from:
$ cd ~/projects/angular/current_project
Run the server:
$ http-server
which will produce the message Starting up http-server, serving on:
Available on:
http://your_ip:8080 and
http://127.0.0.1:8080
That allows you to use urls in your browser like
http://your_ip:8080/index.html
It works best this way. Make sure that both files are on the server. When calling the html page, make use of the web address like: http:://localhost/myhtmlfile.html, and not, C::///users/myhtmlfile.html. Make usre as well that the url passed to the json is a web address as denoted below:
$(function(){
$('#typeahead').typeahead({
source: function(query, process){
$.ajax({
url: 'http://localhost:2222/bootstrap/source.php',
type: 'POST',
data: 'query=' +query,
dataType: 'JSON',
async: true,
success: function(data){
process(data);
}
});
}
});
});
REM kill all existing instance of chrome
taskkill /F /IM chrome.exe /T
REM directory path where chrome.exe is located
set chromeLocation="C:\Program Files (x86)\Google\Chrome\Application"
cd %chromeLocation%
cd c:
start chrome.exe --allow-file-access-from-files
change chromeLocation path with yours.
save above as .bat file.
drag drop you file on the batch file you created. (chrome does give restore pages
option though so if you have pages open just hit restore and it will work).
You can also start a server without python using php interpreter.
E.g:
cd /your/path/to/website/root
php -S localhost:8000
This can be useful if you want an alternative to npm, as php utility comes preinstalled on some OS' (including Mac).
For all python users:
Simply go to your destination folder in the terminal.
cd projectFoder
then start HTTP server
For Python3+:
python -m http.server 8000
Serving HTTP on :: port 8000 (http://[::]:8000/) ...
go to your link: http://0.0.0.0:8000/
Enjoy :)
I am new to node.js. I followed the download and installation instruction from Here.
Problem is now when I try this in my terminal
node
I get following message
No script was specified.
Usage: node [options] script.js [arguments]
Options:
-v, --version print node's version
--debug[=port] enable remote debugging via given TCP port
without stopping the execution
--debug-brk[=port] as above, but break in script.js and
wait for remote debugger to connect
--v8-options print v8 command line options
--vars print various compiled-in variables
Enviromental variables:
NODE_PATH ':'-separated list of directories
prefixed to the module search path,
require.paths.
NODE_DEBUG Print additional debugging output.
Documentation can be found at http://nodejs.org/api.html or with 'man node'
After that I tried this example :
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
It says : ReferenceError: console is not defined.
I googled this and found that i havent installed nodejs properly so I installed it again but no success :(
Please help !!
The version in the installation manual is too old.
Do this
wget http://nodejs.org/dist/node-v0.4.6.tar.gz
gunzip node-v0.4.6.tar.gz
tar -xf node-v0.4.6.tar
cd node
./configure
make
sudo make install
BTW, here are official installation instructions: https://github.com/joyent/node/wiki/Installation