NodeJS+ExpressJS+SocketIO+MongoDB应用模板

OS:Win8.1 with update

关键字:NodeJS,ExpressJS,SocketIO,MongoDB。

 

1.源代码下载:https://github.com/ldlchina/ESMNodeAppTemplate

 

2.服务器环境配置:

安装NodeJS: http://www.nodejs.org/
安装mongodb: http://www.cnblogs.com/ldlchina/p/3551334.html。
打开文件\lib\conf.json, 配置db选项。

{
    "build": "dev",
    "port": "8443",
    "secure": true,
    "dev": {
        "db": {
            "host": "localhost",
            "port": 27017,
            "name": "mydb",
            "collections": {
                "FILE": "file"
            }
        },
        "node_env":"development"
    },
    "prod":{
        "db": {
            "host": "localhost",
            "port": 27017,
            "name": "mydb",
            "collections": {
                "FILE": "file"
            }
        },
        "node_env":"production"
    }
}

打开\package.json, 更改name, version等。

{
  "name": "ESMNodeAppTemplate",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node start"
  },
  "dependencies": {
    "body-parser": "~1.9.2",
    "dateformat": "~1.0.8",
    "express": "~4.10.0",
    "formidable": "^1.0.15",
    "mongodb": "~1.4.19",
    "nconf": "~0.6.9",
    "node-uuid": "~1.4.1",
    "serve-favicon": "^2.1.6",
    "socket.io": "~1.2.0"
  }
}

在\routes的子文件夹下面添加路由。

把所有前端文件放到\frontend文件夹下面。

 

3.启动运行服务器

打开CMD,切换到文件目录\ESMNodeAppTemplate。

第一次启动,运行CMD:“npm install”,安装node modules。

运行CMD:“node start”,服务器启动成功,显示:start at port:8443

 

4.例子

在浏览器窗口打开:http://localhost:8443/samples/testsocketio.html,显示如下:

在浏览器窗口打开:http://localhost:8443/samples/testfileupload.html,显示如下:

posted @ 2014-11-06 23:25  Ldlchina  阅读(1082)  评论(0编辑  收藏  举报