4
var sys = require("sys"),  
my_http = require("http");  
my_http.createServer(function(request,response){  
    sys.puts("I got kicked");  
    response.writeHeader(200, {"Content-Type": "text/plain"});  
    response.write("Hello World");  
    response.end();  
}).listen(8080);  
sys.puts("Server Running on 8080");

The above is my basic web-server, now i want to run my application which contains a HTML and a JS file. Where would i place those files so that i can access it through my port.

I use Apache and Xampp, so i place my files in htdocs directory and access it via my browser, but in terms of node.js i am totally confused?.

3 Answers 3

12

Let's get step by step here.

Identify the location for your application.

Firs identify the location where for your application. Let's take it as C:\your_app. The path doesn’t matter, so feel free to locate the directory wherever is best for you.

Installing Node.js

Here is where we will setup Node.js and Express. Node.js is a framework and Express provides a web server. The web server we need does not need to do anything fancy. The only feature that the web server needs is the ability to provide static files.

To get started download and install Node.JS: http://nodejs.org/

Install Express

Express is a package that execute within Node.js. To install express, in the Command Prompt navigate to your directory for the application which is c:\your_app.

Now lets install Express as a package for Node.js. At the command prompt type “npm install express”. That installed Express and should have created a directory called “node_modules”.

server.js

Now that Express is installed, we need to configure it to execute as a webserver. Create another file in the c:\your_app directory call “server.js”.

var express = require('express');
var app = express();
port = process.argv[2] || 8000;

app.configure(function () {
    app.use(
        "/", //the URL throught which you want to access to you static content
        express.static(__dirname) //where your static content is located in your filesystem
    );
});
app.listen(port); //the port you want to use
console.log("Express server running");

Start Express Web Server in Node.js

In the Command Prompt confirm you are at the c:\your_app directory and execute the following commend.

node server.js 8000

Now the web server should be running on port 8000 and your index.html page should be displayed in the browser.

Sign up to request clarification or add additional context in comments.

Comments

1

You can put the files wherever you like, so long as the user the server is running as can read them.

If you want that code to serve them, however, then you will need to replace all the response.* code you have with code that will:

  1. Identify which file is being requested based on the data in request
  2. Determine if that file exists (and send a 404 response if it does not)
  3. Determine the correct content type for that file type and set the header appropriately
  4. Read the file in and output it in the response

In other words: Node.js is not a web server. You can write a web server in JavaScript and run it on Node.js, but you've only taken the very first steps along that route.

2 Comments

I am now totally confused, "In other words: Node.js is not a web server. You can write a web server in JavaScript and run it on Node.js, but you've only taken the very first steps along that route." What does Node.js do actually, so far i was thinking its like apache where you can run JS code on server side...
It lets you run JavaScript code on a computer. If you write JavaScript code that listens on a network port (i.e. if you write a server) then you are run JS as server side code on the WWW.
1

You don't need Apache to work with Node.js. If you want a basic server, you can use Connect middleware:

var connect = require('connect');
var port = process.env.PORT || 8080;

connect()
  .use( connect.static(__dirname + '/public') )
  .use( function (request, response) {
    /* your code */
  })
  .listen(port);

Create public directory along with your js file, put static files there and fire a server with

$ node index.js

If you don't have Connect installed:

$ npm install connect --save

4 Comments

public directory??? where is that in windows... i am sorry, but i am confused... and what code should i put inside the anonymous use function.
public directory is just a public folder inside your project. Nothing special. Connect or Express is the standard way to create/run a web-server in Node.js
the public directory would be one that you create, located in relation to your index.js file
I recommend exploring express, which uses connect. By following the basic expressJS installation it will create a project folder that contains various folders and configurations in your app.js file that will make it easy to get started

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.