在Windows使用NodeJs和Web Framework:Express开发网站

Windows使用NodeJsWeb Framework:Express开发网站

以下[*]开头注明都在命令行【
CMD】工具中执行。【通过Windows操作系统的[运行]键入cmd打开命令行窗口】
必备条件:
1.在
Windows操作系统上安装NodeJsNpm
2.安装Express框架:
  *.
npm install -g express【安装Express框架】

创建
Web网站:
  1.【创建应用:
betterlife
  *.
express D:\nodejs\betterlife && D:\nodejs\betterlife
  2.安装Express应用运行必需【Dependency】模块
  *.npm install
  3.运行服务器
  *.node app
  一般默认会启动3000端口
到这一步应该就可以在电脑的浏览器里看到该页面。

    



-------------------------前进前进前进进-----------------------------------
1.安装Nodemon,修改所见即所得,无需重启Node服务器
  *.npm install -g nodemon
2.在Github上下载一个Node应用:https://github.com/shapeshed/express_example
3.解决中文乱码的问题
  将下载包里后缀名为jade的文件另存为Utf8,这样修改内容为中文,也不会有中文乱码的问题
4.寻找云部署站点[免费]:
  Heroku:https://devcenter.heroku.com/articles/quickstart
5.以下是我部署成功的NodeJs站点
  采用Heroku部署成功:
      URL:http://nodejsapp.herokuapp.com/
      Git:git@heroku.com:nodejsapp.git

-------------------------创建静态服务器-----------------------------------

第一种方案:自己创建一个server.js
1.npm以下包:

 *.npm install http
 *.npm install url
 *.npm install path
 *.npm install fs

2.创建server.js文件,代码清单如下:
************************************代码开始*******************************************

var http = require("http");
var url = require("url");
var path = require("path");
var fs = require("fs");

//发布端口
var port=1234;

//发布路径
var publish_path="D:\\dev\\nodejs\\content";

//允许访问的MIME类型
var mime = {
  "css": "text/css",
  "gif": "image/gif",
  "html": "text/html",
  "ico": "image/x-icon",
  "jpeg": "image/jpeg",
  "jpg": "image/jpeg",
  "js": "text/javascript",
  "json": "application/json",
  "pdf": "application/pdf",
  "png": "image/png",
  "svg": "image/svg+xml",
  "swf": "application/x-shockwave-flash",
  "tiff": "image/tiff",
  "txt": "text/plain",
  "wav": "audio/x-wav",
  "wma": "audio/x-ms-wma",
  "wmv": "video/x-ms-wmv",
  "xml": "text/xml"
};

/**
 * URL 路由
 */
function route(request, response){
    var pathname = url.parse(request.url).pathname;
    var realPath = publish_path+pathname;
    
    path.exists(realPath, function (exists) {
        if (!exists) {
            response.writeHead(404, {'Content-Type': 'text/plain'});
            response.write("This request URL " + pathname + " was not found on this server.");
            response.end();
        } else {
            fs.readFile(realPath, "binary", function(err, file) {
                if (err) {
                    response.writeHead(500, {'Content-Type': 'text/plain'});
                    response.end(err);
                } else {
                    var ext = path.extname(realPath);
                    ext = ext ? ext.slice(1) : 'unknown';
                    var contentType = mime[ext] || "text/plain";
                    response.writeHead(200, {'Content-Type': contentType});
                    response.write(file, "binary");
                    response.end();           
                }
             });
          }
    });  
}

/**
 * 运行服务器
 */
function server_run(){
  function onRequest(request, response) {
    route(request, response);
  }
  http.createServer(onRequest).listen(port);
  console.log("Server is running on port "+port+".");
}

server_run();
************************************代码结束*******************************************
3.运行server.js

node server.js

第二种方案:使用Express框架,定义server.js

TODO


参考文献:
Web Framework: Express:https://github.com/visionmedia/express
Express参考手册:http://expressjs.com/guide.html
Creating a Basic Site With node.js and Express:
http://shapeshed.com/creating-a-basic-site-with-node-and-express/ 

用NodeJS打造你的静态文件服务器:

http://cnodejs.org/topic/4f16442ccae1f4aa27001071

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值