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

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

Related

Why we need http module installed to run our node js application?

I have found so many sources for now when the first application shows this line
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);
Just being geek, my Question is why we need server/port to listen our requests for our node js applications?
Why can't we run as localhost/application_name instead?
Why we need that?
Can anyone elobarate please?
Node.jsĀ® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
So if you want an application which only work with bash you don't need any http modules.
Browsers use HTTP. So if you want to develop a web application you need to use that protocol. If you run your project on 80 port you can use it like localhost/my_application.
Simple app.js
var result = doSomething();
functions doSomething(){
return "This the result";
}
console.log(result);
You can call it from bash. node app.js. But it just work and stop.
But if you want to serve this structure to WWW (which is using HTTP) you need to create server. http is a great and simple module for creating servers with node.js.
You can use other js files with using require.
app.js
var result = doSomething();
functions doSomething(){
return "This the result";
}
module.exports = result;
server.js
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var result = require('app.js');
res.end(result);
}).listen(80);
Now you can run your server. node server.js
You can run arbitrary javascript with node. The code you've provided specifically sets up an http server that listens on port 8080. You can reach that webserver from a browser on the same computer by browsing to http://localhost:8080.
We don't need to install 'http' module in order to use it, it is already there in nodejs framework itself.
If you want to see output of any programming language you serve it as http because you want your browser to reach your server. Like what you do it php built in server php -S localhost:8081 or serve it via nginx or apache
If you don't serve your JS, PHP, Python ... over http, browser will treat those files as other unsupported file like a .tar file.
Node is JavaScript environment, Not a web server. You need a server to serve your application. You may use http, https or you can create any other server that can serve your js file.
Well, I do not know if my answer is clear enough to explain but hope you will have some idea why you use http module in your nodejs application.

Applying node.js server to a web server

I am trying to learn how to use Node.js and web sockets to create simple multi-user interactive javascript programs. I used this tutorial series by Daniel Shiffman to create this example project. My next step would be to upload it, using WinSCP, to my RaspberryPi apache2 web server, but I haven't found a way to edit the code in a way to allow for that to work, and furthermore, I do not know what piece of the programs to execute to make it function properly.
Any assistance would be great. The extent of my Node / Socket.io knowledge comes entirely from the video series mentioned above, so you can assume I know almost nothing else.
Apache is a web server and it serves your file and send them to client for you, so when you have some client side things like html site with some css, javascript and images you can use apache to send them to client for you.
In node.js you can create this web server simply by following code and express library:
// Create the app
var app = express();
// Set up the server
var server = app.listen(3000, () => {
console.log('http server is ready')
});
as you created in your code too. by this web server you can host your files and do many more things like setup socket.io server and ... because you write web server yourself. with following code you serve static files in public directory (html, css, javascript and images ...):
app.use(express.static('public'));
after you finishing this process you can run it simply by:
npm install
node server.js
if you want you can run you code inside docker by creating Dockerfile and ...
About your question, you must move all your project files into raspberry and at the end you have following directory tree in somewhere in raspberry:
|- server.js
|- package.json
\ public
at this directory run above commands and your server will be up and running and you can access to it by http://raspberry_ip:3000.

How to access remote node.js app in browser, not on localhost

I'm trying to set up a simple "Hello world" node.js app.
I've created the following index.js file:
var app = require("express")();
var http = require("http").Server(app);
app.get("/", function(req, res){
res.send("<h1>Hello worlddddd</h1>");
});
http.listen(8080, function(){
console.log("listening on *:8080");
});
When I open up my local console, and perform node index.js, I get the message "listening on *:8080", as expected. I point my browser to localhost:8080, and I see the HTML page saying "Hello worlddd", as desired.
Now, I'm trying to do the same on my Virtual Private Server, so I can access the same app from different computers, but all I get is connection timeouts. I've followed these steps:
Install node.js on my VPS
Install express via npm install --save express#4.10.2
Upload my index.js file to the var/www/html folder on my server with IP 192.123.123.12 (an example, this isn't my real IP).
Access the server via PuTTY, and run node index.js, where I get "listening on *:8080", so I know node.js is working.
Now I point my browser to http://192.123.123.12:8080 and after about 20 seconds, I get the browser error: "The connection has timed out".
I've tried listening to port :80 instead, but I get the error that this port is already in use.
Does anybody know what I'm doing wrong? Am I using the wrong port? Am I pointing to the wrong URL? Do I need to modify my server preferences? (running Apache on CentOS). I've only found dozens of tutorials that teach you how to run a node.js app on your local computer(pointing the browser at localhost:8080), but I need it to run on my remote server so multiple computers can access the same app.
The issue is that your current filters (iptables) block traffic unless you explicitly allow it.
You just need to open port TCP 8080 inbound, and you should be able to reach your node.js server!

Point domain to node express server on Azure

This must be an extremely common problem. I've seen various answers for this but none seem to work for me.
I have node installed on an apache server on Windows Azure. My app is built and ready to go (snippet below):
var express = require("express");
var app = express();
//example api call
app.get("/api/example", function(req, res){
//do some process
res.send(data);
});
app.listen(8080);
console.log("App listening on port 8080");
Now, when testing on my own computer, I could then go to localhost:8080, which works great. But now I've put it on the azure server I can't get an external domain to point to it properly. So for example, I have the domain:
framework.example.com
I've added this to my hosts file in Azure:
XXX.0.0.01 framework.example.com
Initially I tried also editing the http-vhosts.conf to point the domain to the correct directory. This worked for loading the frontend, but the app couldn't talk to the backend. API calls returned 400 not found errors.
I've also tried an Express vhost method but think I'm doing it wrong and don't fully understand it. What is the correct method?!
My app structure is like this:
- package.json
- server.js
- server
- files used by server.js
- public
- all frontend files
So to boot the server I run server.js which runs the code at the top. The server.js uses the below Express config to point to the public folder.
app.use(express.static(__dirname + "/public"));
Adding it to the hosts file in Azure won't help. You'll need to configure your domain's DNS to point to Azure. I'd recommend using the DNS Name of your Cloud Service instance. Your underlying VM IP address could change if you need to stop it for some reason, but your Cloud Service DNS name is configured to always route to your underlying VMs. That means you'll need to setup a CNAME with your DNS.
Read more about that here: Cloud Services Custom Domain Name
Next, you'll either need to host the node app on port 80, or put a proxy in front of it to handle that for you. Otherwise you'll be stuck typing framework.example.com:8080 which is not ideal. On linux, you'll likely need to be a privileged user to host on port 80, but you never want your node app to have root privileges. You can use authbind to work around this problem.
See an example of how to use it with node here: Using authbind with Node.js
All that being said, it seems like you're somewhat new with linux server management. If that's the case, I'd strongly recommend trying to use something like Azure Websites instead of a VM. You no longer have to manage the virtual machine OS. You simply tell it to host your application and it takes care of the rest. If you're using github, this is incredibly easy to test and iterate with. It does host on Windows under the hood, and that might problems for some applications, but I host all my node sites there (developed on Mac) without any issues.

How to write first application in Node.js in Windows Environment?

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/

Categories

Resources