How I can run this script in node.js? - javascript

I have install all modules need to run this script using CMD. When I am run this script in node.js through webmatrix:
var http = require('http');
var everyauth = require('everyauth');
var app = require('express').createServer();
app.get('/', function(request, response) {
response.send('Hello Express!!');
});
app.listen(2455);
It's not working. The error is:
iisnode encountered an error when processing the request.
HRESULT: 0x2
HTTP status: 500
HTTP reason: Internal Server Error
You are receiving this HTTP 200 response because system.webServer/iisnode/#devErrorsEnabled configuration setting is 'true'.
In addition to the log of stdout and stderr of the node.exe process, consider using debugging and ETW traces to further diagnose the problem.
The node.exe process has not written any information to the stdout or stderr.
I have tried with different different script from blog but they do not work. I tried Steven Sanderson's template in webmatrix and that does work.

To use your application with iisnode, you have to use port/socket supplied by iisnode (as in this case you're using IIS as a webserver instead of the one bundled in node).
Replace the last line with app.listen(process.env.port || 2455);, so that it will both work well with iisnode and be available on port 2455 when run with node.exe app.js.

Use
app.listen(process.env.port || 2455);
instead of `
app.listen(portno);
cause it's iisnode web server.

Related

Failed to connect to localhost port 8000: Connection refused in Node.js

I'm trying to connect to the HTTP server in node.js.
I've spent my whole day searching online to solve my issue but unfortunately, it is of no use.
This is a simple code I wrote taking reference from official Node.js documentation.
Index.js
const http = require('http');
const requestListener = function (req, res) {
res.writeHead(200);
res.end('Hello, World!');
}
const server = http.createServer(requestListener);
server.listen(8080);
The code I am running inside CMD terminal
curl localhost:8080
This returns the following error:
I know this questions have been asked a thousands time in stack overflow but none fixed my issue. Anyone can help me?
In you code you are listening to port 8080 but from commandline you are trying 8000. Make sure you are using correct port and ensure its not in use.
You are trying to execute C:\Users\prabi\app\server.js when the real file is C:\Users\prabi\app\index.js so verify the start script in package.json and fix it.
Fire up another terminal and run the command:
node index.js
Then go back to your previous terminal window/tab and run curl localhost:8080 again. Should work.
The problem is that your code has been written well, but you aren't executing it at all. So if you don't run your node code, your http server cannot listen at the 8080 port in the first place.

how to make a node js run a js file and issues creating a js file in node.js console window with win10

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)

Why is NodeJS only showing files on my browser instead of the results of my code?

I'm new into node JS. I'm trying to install node and run just a 'Hello World' app but I have problems with my server.
When I try to run my app, the server is showing only the index files and not 'Hello World'. enter image description here
The node server is working and says that the http-server is available.
My code for my Hello World app:
var http = require("http");
http.createServer(function(req, res){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8080);
console.log('Server running at http://192.168.178.14:8080/');
I want to run the file 'index.js' (second file in first screenshot). How can I fix this?
As Mikael Lennholm and robertklep have mentioned in the comments, the issue is that you are trying to run a second server on the same address (:8080).
I tested this myself to make sure, and here's the results I got:
Running an instance of the ecstatic server on :8080, producing a very similar image to yours (using default code provided by ecstatic's documentation).
Attempting to run a regular server on the :8080 port gives me an error, EADDRINUSE :::8080 (which is expected, as the other application is already using that address). You should probably get this error when trying to run your application as well.
Now, either you or someone else is to blame. But the fact is that there is already a server running on port 8080. I'd reccomend you try to find out who set it up (if it wasn't yourself) or maybe just try using another port. For example, 8088:

how to run node.js on windows with apache server installed in?

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

HTTP Server on Android via node.js errors upon request

I'm running node.js v0.11 on Android (via https://github.com/paddybyers/node). I attempted to try out the "Hello HTTP" example found here: http://howtonode.org/hello-node, however, I ran into problems.
The server starts fine, but as soon as I attempt to connect to the http server (by visiting http://localhost:8000/), I get this error:
net.js:1156
COUNTER_NET_SERVER_CONNECTION(socket);
^
ReferenceError: COUNTER_NET_SERVER_CONNECTION is not defined
at TCP.onconnection (net.js:1156:3)
My code is exactly the same as the Hello HTTP example:
//Load the http module to create an http server.
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/");
How can I fix this?
Thanks!
Compile Node.js with the HAVE_PERFCTR option. (You Need to implement your own perfctrs for Android. See node_counters.cc line 130).
It should be also possible to define the missing functions in your script as empty functions.
PS.:
You also need DTRACE.
I simply commented out all COUNTER_NET_, COUNTER_HTTP_, DTRACE_NET_, and DTRACE_HTTP_ calls in the javascript files under lib/. This amounted to about 10 or so lines that I commented out of net.js and http.js.
I think that js2c.py is supposed to process src/macros.py and src/perfctr_macros.py to effectively do this 'commenting out' for you when it serializes the scripts under lib/ to C arrays in out/release/src/node_natives.h, but that doesn't seem to be happening.

Categories

Resources