安装node js+express+mongodb

1.nodejs

官网地址:http://nodejs.org

2.express

$ npm install -g express-generator@4(不再是之前的npm install -g express)

3.利用express建立web工程(模板引擎使用ejs,express默认是jade)

express -e ejs microblog (不再是之前的express -t ejs microblog)
模板引擎是一个从页面模板根据一定的规则生成HTML的工具,即建立一个HTML页面模板,插入可执行的代码,运行时动态生成HTML.目前的主流是服务器运行模板引擎。

4.安装依赖

cd microblog && npm install

5.启动express建立的工程

express4.x 的启动是 npm start 不是之前的node app.js

6.路由对应

官网:http://expressjs.com/en/guide/routing.html
两种方式:
1.

var express = require('express');
        var app = express();
     //This route path will match requests to the root route, /.
  app.get('/', function (req, res) {
              res.send('root');
});

//This route path will match requests to /about.
app.get('/about', function (req, res) {
      res.send('about');
});

2.Use the express.Router class to create modular, mountable route handlers. A Router instance is a complete middleware and routing system; for this reason, it is often referred to as a “mini-app”.

The following example creates a router as a module, loads a middleware function in it, defines some routes, and mounts the router module on a path in the main app.

Create a router file named birds.js in the app directory, with the following content:

var express = require('express');
var router = express.Router();

// middleware that is specific to this router
router.use(function timeLog(req, res, next) {
  console.log('Time: ', Date.now());
  next();
});
// define the home page route
router.get('/', function(req, res) {
  res.send('Birds home page');
});
// define the about route
router.get('/about', function(req, res) {
  res.send('About birds');
});

module.exports = router;

Then, load the router module in the app:

var birds = require('./birds');
...
app.use('/birds', birds);

The app will now be able to handle requests to /birds and /birds/about, as well as call the timeLog middleware function that is specific to the route.

7.安装mongodb

官网:https://www.mongodb.org/
安装步骤:http://jingyan.baidu.com/article/d5c4b52bef7268da560dc5f8.html

8.使用过程中存在的和之前版本的不同点

1)配置connect - mongo

var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var settings = require('./settings');
var flash = require('connect-flash');

app.use(session({
   secret:settings.cookieSecret,
   cookie:{maxAge: 1000*60*60*24*30},//30days
   store: new MongoStore({
       db:settings.db,
    host: settings.host,
    port: settings.port
   })
})
 );
...

2)在app.js中使用locals 存放页面中访问到的变量,代替视图助手

app.use(function(req,res,next){

      res.locals.user = req.session.user;
      console.log("user: ");
      console.log(req.session.user);
      var error = req.flash('error');
      res.locals.error = error.length?error:null;

      var succ = req.flash('success');
      res.locals.success = succ.length?succ:null;

      next();
});

3)db.js 中找不到Connection.default_port 直接用27017

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值