javascript 使用 express.js 和 node.js 运行我的示例应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18784873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
running my sample app using express.js and node.js
提问by Kevin
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.
以上是我的基本网络服务器,现在我想运行我的应用程序,其中包含一个 HTML 和一个 JS 文件。我会将这些文件放在哪里,以便我可以通过我的端口访问它。
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?.
我使用Apache and Xampp
,所以我将我的文件放在htdocs
目录中并通过我的浏览器访问它,但就node.js
我而言,我完全感到困惑?。
回答by Thalaivar
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.
首先确定您的应用程序的位置。让我们把它当作C:\your_app
. 路径无关紧要,因此您可以随意找到最适合您的目录。
Installing Node.js
安装 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.
这是我们将设置的地方Node.js and Express
。Node.js 是一个框架,Express 提供了一个 Web 服务器。我们需要的 Web 服务器不需要做任何花哨的事情。Web 服务器需要的唯一功能是提供静态文件的能力。
To get started download and install Node.JS: http://nodejs.org/
要开始下载并安装 Node.JS:http://nodejs.org/
Install Express
安装 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.
Express 是一个在 Node.js 中执行的包。要安装 express,请在命令提示符中导航到应用程序的目录 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”
.
现在让我们install Express as a package for Node.js.
在命令提示符下键入“npm install express”
. 那安装了 Express 并且应该创建了一个名为“node_modules”
.
server.js
服务器.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”.
现在 Express 已安装,我们需要将其配置为作为网络服务器执行。在 c:\your_app 目录中创建另一个文件,调用“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
在 Node.js 中启动 Express Web Server
In the Command Prompt confirm you are at the c:\your_app directory and execute the following commend.
在命令提示符中确认您位于 c:\your_app 目录并执行以下命令。
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.
现在 Web 服务器should be running on port 8000
和您的 index.html 页面应该显示在浏览器中。
回答by Quentin
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:
但是,如果您希望该代码为他们服务,那么您需要用以下response.*
代码替换您拥有的所有代码:
- Identify which file is being requested based on the data in
request
- Determine if that file exists (and send a 404 response if it does not)
- Determine the correct content type for that file type and set the header appropriately
- Read the file in and output it in the response
- 根据中的数据确定正在请求哪个文件
request
- 确定该文件是否存在(如果不存在则发送 404 响应)
- 确定该文件类型的正确内容类型并适当设置标题
- 读入文件并在响应中输出
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.
换句话说:Node.js 不是 Web 服务器。您可以使用 JavaScript 编写 Web 服务器并在 Node.js 上运行它,但您只是沿着这条路线迈出了第一步。
回答by Pavlo
You don't need Apache to work with Node.js. If you want a basic server, you can use Connectmiddleware:
您不需要 Apache 来使用 Node.js。如果你想要一个基本的服务器,你可以使用Connect中间件:
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
public
与您的 js 文件一起创建目录,将静态文件放在那里并使用以下命令启动服务器
$ node index.js
If you don't have Connect installed:
如果您没有安装 Connect:
$ npm install connect --save