流水下我的express项目的初始化

/ 0评 / 0

虽然这个东西可以用Yeoman来替代我这种傻逼操作,但是我还是觉得有必要流水下。

STEP-1
express -s -e MyProject
cd MyProject
vim package.json

{
    "name": "MyProject",
    "version": "0.0.1",
    "private": true,
    "scripts": {
        "start": "node app.js"
    },
    "dependencies": {
        "connect-redis": "^1.4.7",
        "ejs": "*",
        "express": "3.4.4",
        "moment": "^2.8.1",
        "mysql": "^2.3.2",
        "passport": "^0.2.0",
        "passport-local": "^1.0.0",
        "redis": "^0.10.3",
        "sequelize": "^1.7.8"
    },
    "devDependencies": {
        "grunt": "~0.4.1",
        "grunt-contrib-jshint": "~0.6.0",
        "grunt-contrib-nodeunit": "~0.2.0",
        "grunt-contrib-uglify": "~0.2.2",
        "grunt-contrib-copy": "~0.4.0"
    }
}

cnpm install

STEP-2
vim app.js

var express = require('express');
var routes = require('./routes/index');
var http = require('http');
var path = require('path');
var db = require('./core/models');
var passport = require('./config/passport');
var RedisStore = require('connect-redis')(express);

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.urlencoded());
app.use(express.cookieParser());
app.use(express.session({
    secret: 'yoursecret',
    store: new RedisStore({
    host: '127.0.0.1',
    port: '6379'
    })
}));

app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
    app.use(express.errorHandler());
}

routes(app);

db
    .sequelize
    .sync({force: false})
    .complete(function(err) {
        if (err) {
        throw err[0]
        } else {
        http.createServer(app).listen(app.get('port'), function(){
            console.log('Express server listening on port ' + app.get('port'))
            })
        }
    });

STEP-3
mkdir config
cd config
vim passport.js

var passport = require('passport'),
    LocalStrategy = require('passport-local').Strategy,
    db = require('../core/models'),
    md5 = require('../core/utils').md5;

passport.use('local', new LocalStrategy(
    function (username, password, done) {
    db.User.find({where: {username: username}})
        .success(function(user) {
            if (user) {
                if (username !== user.username) {
                    return done(null, false, { message: 'Incorrect username.' });
                }

                if (md5(password) !== user.password) {
                    return done(null, false, { message: 'Incorrect password.' });
                }

                return done(null, user);

                } else {
                    return done(null, false, { message: 'Incorrect user' });
                }
            }).error(function(err) {
                return done(null, false, { message: err});
            });
        }
));

passport.serializeUser(function (user, done) {//保存user对象
    done(null, user);//可以通过数据库方式操作
});

passport.deserializeUser(function (user, done) {//删除user对象
    done(null, user);//可以通过数据库方式操作
});

module.exports = passport;

STEP-4
cd ..
mkdir core && cd core
vim utils.js

var crypto = require('crypto');

exports.md5 = function (text) {
      return crypto.createHash('md5').update(text).digest('hex');
};

STEP-5
vim models.js

var Sequelize = require('sequelize'),
  sequelize = new Sequelize('databaseName', 'username', 'password');

module.exports = {
    sequelize: sequelize
  , Sequelize: Sequelize
}

STEP-6
cd ../routes
vim index.js

var user = require('./user');

module.exports = function(app) {
  app.get('/', function(req, res) {
      res.send('ok');
    });

    user(app);
};

vim user.js

module.exports = function(app) {
  app.post('/user', function(req, res) {
      res.send('user');
    });
};

FINAL
connect-redis有可能会版本没装对,如果出现RedisStore.prototype.__proto__ = Store.prototype;的报错,就把connect-redis删了,重新用命令安装一遍cnpm install [email protected]

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注