最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - running my sample app using express.js and node.js - Stack Overflow

programmeradmin0浏览0评论
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?.

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?.

Share Improve this question edited Sep 13, 2013 at 13:28 Thalaivar 23.6k5 gold badges62 silver badges71 bronze badges asked Sep 13, 2013 at 11:09 KevinKevin 23.6k26 gold badges84 silver badges112 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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.

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.

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
发布评论

评论列表(0)

  1. 暂无评论