GithubHelp home page GithubHelp logo

hhy5277 / express-starter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from i5ting/express-starter

0.0 1.0 0.0 199 KB

a nodejs express boilerplate project for starter

Home Page: https://github.com/i5ting/express-starter

JavaScript 88.38% Shell 3.06% CSS 0.70% HTML 7.86%

express-starter's Introduction

Express Starter

  • use express 4.*
  • use jade
  • use mongoose
  • use config.json
  • use session
  • use bluebird for promise/A+
  • use mocha for test
  • add startmongodb.sh && start.sh
  • add pm2 for deploy
  • use supervisor for live reload
  • add multer(2015.5.12 07:09)
  • plain post(2015.5.12 07:09)
  • add cors(2015.5.12 07:19)
  • add mongodb session store

todo

目录说明

  • m (增加models目录)
  • v (使用默认的views)
  • c (使用routes路由 + actions具体业务逻辑)
  • bin 是系统可执行脚本存放位置目录
  • test 是默认测试目录
  • config 是配置项目录,比如db信息等
  • middleware 是express中间件目录,默认有一个检查session的中间件

pm2 tips

启动项目

$sudo pm2 start app.js -i max --name 'express-starter'

查看状态

$sudo pm2 status

停止所有

$sudo pm2 stop all

技术点

multer上传

express 4 官方上传中间件

https://github.com/expressjs/multer

var app = express();
var multer = require('multer');

app.use(multer({ 
  dest: './uploads/',
  rename: function (fieldname, filename) {
    return filename.replace(/\W+/g, '-').toLowerCase() + Date.now()
  }
}));

plain post

3种不同的post

  • Post with x-www-form-urlencoded
  • Post with form-data
  • Post with raw

头2种express是内置处理的,第三种要自己处理

var app = express();

app.use(function(req, res, next){
  if (req.is('text/*')) {
    req.text = '';
    req.setEncoding('utf8');
    req.on('data', function(chunk){ req.text += chunk });
    req.on('end', next);
  } else {
    next();
  }
});

测试方法

$echo -n 'post raw json'
$curl -d "{"a":"1","b":"2","c":{"a":"1","b":"2"}}" http://127.0.0.1:3001/users/post

更多见 https://github.com/i5ting/node-http

支持跨域cors

// 支持跨域
app.use(require('cors')());

session

https://github.com/kcbanner/connect-mongo

项目使用mongoose做操作mongodb的库,所以这里直接复用其连接即可

见app.js里

var session      = require('express-session')
var MongoStore   = require('connect-mongo')(session);
var mongoose     = require('mongoose');

....

var half_hour = 3600000 / 2;

app.use(session({
  store: new MongoStore({ mongooseConnection: mongoose.connection }),
  secret: 'gupjia.ng@me',
  resave: false,
  saveUninitialized: true,
  cookie: {
    secure: false,
    maxAge: half_hour
  }
}));

安装mongoose经常会报错的解决方案

问题在bson

原因是mongoose依赖mongodb,mongodb里依赖mongodb-core,mongodb-core依赖bson

解决方案

$sudo npm install -g mongooseinstaller
$mi

express-starter's People

Contributors

albin3 avatar i5ting avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.