I have downloaded Node.js from their site in my windows xp os. I have installed it through the microsoft installer. I didn't know how to write my first application, where to save them and how to execute them in Windows. I have got a node.js command prompt but I can't use it.
I have searched a lot but there is only instruction for linux and mac. I didn't find any suitable tutorial or example that I can start a node application from scratch.
If anybody can put some documentation or steps or any such tutorial where I can get help of this, it will be great for me.
As this blog entry by Eric, it is ridiculously easy to get a node.js server setup and responding to requests on 127.0.0.1:#port#.
Really easy. I just did it all in... Longer than write this text.
Download an OS-appropriate node.js1: http://nodejs.org/
Now, create a .txt (plaintext) file in the same folder as your node.exe and rename that to server.js.
Put this Javascript in that file:
var http = require('http');
http.createServer(function (request, response) {
console.log('request starting...');
response.writeHead(200, { 'Content-Type': 'text/html' });
var html = '<p>Hello World!</p>';
response.end(html, 'utf-8');
}).listen(8125);
console.log('Server running at http://127.0.0.1:8125/');
Open a cmd.exe and cd c:\path\to\the\node\executable, where node.exe resides.
From there, run:
node server.js
You should see it say:
Server running at http://127.0.0.1:8125
Open a browser and put http://127.0.0.1:8125/ in the address bar.
You should see it say:
Hello World!
That's it. That's pretty easy. The proverbial Hello World! in 15 seconds or less. Now how to get it to parse a Javascript file and return that output instead of simple HTML...
1. Note, I got the executable, node.exe, made a folder somewhere, put the executable in that folder. No installer. Works like a charm.
Here is the new way develop node.js app with Windows Environments and Visual Studio >= 2012
https://nodejstools.codeplex.com/
Related
I am learning JavaScript development and the book I'm following (JavaScript Everywhere) tells me run this code:
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Hello World'));
app.listen(4000, () => console.log('Listening on port 4000!'));
I understand what this code is supposed to do but when I try to run it using src/index.js in the terminal, it always gives me this error:
Syntax Error 800A03EA Microsoft JScript compilation error
I have tried all fixes I found on the internet but I'm guessing this has something to do with the fact that this isn't regular JS syntax, it's Express JS and somehow Express JS is not enabled? I've checked the basics including:
node --version (returns v12.14.0)
npm --version (returns 6.13.4)
npm install express --save just in case there was some problem with ExpressJS API
Windows 10 Laptop, using VSCode. Please ask for any information you need.
Run it using node src/index.js.
By using just src/index.js you are using the default handler for JS files on Windows which is Windows Script Host (and not Node.js).
The solution apply for those who are facing Windows script Error code like 800A03EA. For those, who are unable to open js file and facing error like Windows Script Host.
I was stuck with the same problem for couple of days while working on react.
We have three methods of solving this problem:
Method 1:
Check your computer for viruses and remove them using various method like download MSERT.exe from Microsoft.
Method 2:
Open command prompt as administrator and type following 2 commands:
regsvr32 jscript.dll and press ENTER.
regsvr32 vbscript.dll and press ENTER.
Method 3:
Type internet option on search bar and select it.
Open advance gtab from it and go to security section.
Make sure all of the below ones are selected:
Use SSL 3.0
Use TLS 1.0
Use TLS 1.1
Use TLS 1.2
Method 4:
The workaround that must work 100% but as you know it might be helpful for all, as it only open file do not confuse read the following lines
So simply left click on file and edit and open it.
It will open in notepad and now from here you can transfer your data or use it.đ
Thanks me later for this detail answer. Now just rock the world!!!!!
Check if your file name isn't "Node.js", just try to rename it in like "randomname.js"
I am a beginner in node js and have a couple of months experience with javascript, have finished codecademy js and jquery introductory courses and learned more about js in the internet and have an understanding of html and css in intermediate level or near to intermediate level. I am a complete noob in getting the js and node js to run alltogether. Using VS code for text editor. Node.js is installed from their original homepage, using node.js APP. File named global js.is sticked to 2 folders: the same with node.js and desktop folder for vs code files( the ones I create in VS code as projects or just simple files). The darn thing just doesn't make sense to me and I dont get this mess to work either. To be more specific, then:
I have 2 issues:
Firstly, did an install: npm install javascript bla bla. It was a sucsess, mkdir and cd were successful. NUL >introduction.js says access denied.
Alos I have a file named global.js. It is sitting in the same foler as node.js + the original version of this fie is in the same folder of desktop vs code's excercises. I can't make my node.js open this global.js file.
How do I overcome of these issues?
I tried to use this as a guide line: How to run a hello.js file in Node.js on windows?.
Did following parts of that above: changed account type to ADMINISTRATOR via appwize smth from run. Called cmd from run after having given myself and admin status permanently and windows logged me off and back on again. Tried to run the file from command prompt, declaring the exact path to file in cmd. It reached to the path, showed no error, went to the file from there. No errors, NOTHING didn't happen... Tried the global install whatever faced an issue in there and got stuck with it.
I need some help in here! Would be nice if someone could explain to me what is wrong and what's the basic concept of using node.js or what are the alternative ways to programmetely launch it?
Do I need to use the node.js console or node.js app?
CODE IN main. js :
var http = require("http");
var path= require ("path");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
console.log (`Rock on World from ${path.basename(__filename)}`);
var http = require("http");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
No need to use any special console, on windows, standard cmd is enough.
Check if node is installed properly first. Run node -v and see if it prints its version.
Create a folder for your project and make sure your are in it before executing node or npm. Node.exe doesn't have to be in the project folder as long as it is in the path (by default it is)
Make sure your source files in that project folder. (test.js, introduction.js .. whatever you will run)
On my server, every time a user uses our service we have to grab a JSON file for them from the server. I do this by using fs.createReadStream() inside of my own function.
function getJSONFromServer(filepath, callback){
var data = fs.createReadStream(filepath);
data.on('error', function (error) {
console.log("Caught", error);
callback(undefined, error);
});
var jsonFile = "";
data.on('data', function(chunk) {
jsonFile += chunk;
});
data.on('end', function() {
var jsonData = JSON.parse(jsonFile);
callback(jsonData);
data.destroy();
data.close();
});
}
This does the job, but it does not close the connection to the file. So after reading 1024 files (the limit on my server), Node.js will then produce the error EMFILE, too many open files. Then I have to kill our Node.js server, open it again and that will clear the "open files".
I check the amount of files open by lsof -i -n -P | grep nodejs. It displays something like this:
nodejs 13707 node 10u IPv4 1163695 0t0 TCP 127.0.0.1:55643->127.0.0.1:27017 (ESTABLISHED)
nodejs 13707 node 11u IPv4 1163697 0t0 TCP 127.0.0.1:55644->127.0.0.1:27017 (ESTABLISHED)
for as many files that are open.
I've tried using graceful-fs. I've tried calling stream.destroy() and stream.close(), but I still get the same issue. My server is essentially a ticking time bomb because we get a heavy, steady flow of users and after so many users have connected it will just stop working.
Also, ulimit -n [open file amount] does not work, and even if it did, this is not a long term solution because I'd like my file connections to close and not sit open for no reason.
I'm using Node.js version v0.10.25, Ubuntu 15.04 (GNU/Linux 3.19.0-42-generic x86_64) and the latest version of graceful-fs if that helps!
Thanks for any help you can provide.
This has got to be the stupidest mistake I've ever made. Regardless, here's the answer. I hope I can save someone from dealing with this error and almost ripping their hair out.
I was running my app with nodejs and not node. Turns out, if you do nodejs --version, it will likely return a version that is very old, which was v0.10.25 for me. node --version however was v5.6.0. Obviously this massive jump in versions would fix some stuff, so I ran the app with node app.js instead of nodejs app.js and I haven't had the issue at all since. There are now only 6 open files, whereas before we had over 1000 with time.
Damn it feels good to have this off my chest.
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'm a node.js begginer . Let's say I have an apache server(XAAMP) and node.js installed in C:\Program Files\nodejs\nodejs.exe on windows 7.
How can I run node.js in my apache server to simulate my code?
I mean, I know how to write node.js code but what I don't know how it's work on my server?
Apache server don't need for Node.js.
For create your own Node.js server:
Download and install Node.js
Create file hello.js:
var http = require("http");
var server = http.createServer().listen(3000); // beter way for create
server.on("request", function(req, res){
res.writeHead(200, {"Content-Type": "text/plain"});
// for view at page http://localhost:3000
res.write("Hello world");
res.end();
});
server.on("listening", function(){
// for view in console
console.log("Listen: 3000...");
});
In terminal go to dir where file hello.js and type:
node hello.js
Open your browser and point it at http://localhost:3000/. This should display a web page that says:
Hello world
A basic HTTP server
Node.js Manual & Documentation
If you like to work with a replacement for XAAMP you should finally take a look at MEAN.io.
At NpmJS.org you will find different solutions for most of your needs.
and like Reagan Gallant commented you should take a look at this famous stackoverflow post (if you need ideas).
NodeSchool indeed is a good entry point for your fist steps. After that npmjs will make sense and finally you will love Mean.io
You just make it use a different port than Apache is using (for example port 3000 which is the default for express-js and others) -- that is assuming that you don't need the two to work together.
If you do need them to work together, you add a forwarding module to Apache and configure the forwarding in Apache of certain URL to go to your local port for node-js