码迷,mamicode.com
首页 > Web开发 > 详细

EXPRESS.JS再出发

时间:2016-02-01 01:44:50      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

那个那个MEAN的书,看得七七八八,有了大概,现在就要一样一样的加深记忆啦。。

EXPRSS.JS的东东,网上有现成入门书籍:

 

第一期代码测试:

 

var express = require(‘express‘);

var app = express();

var handlebars = require(‘express3-handlebars‘)
        .create({defaultLayout: ‘main‘});
app.engine(‘handlebars‘, handlebars.engine);
app.set(‘view engine‘, ‘handlebars‘);


app.set(‘port‘, process.env.PORT || 3000);

app.use(express.static(__dirname + ‘/public‘));

var fortunes = [
    "Conquer your fears or they will conquer you.",
    "Rivers need springs.",
    "Do not fear what you don‘t know.",
    "You will have a pleasant surprise.",
    "Whenever possible, keep it simple.",
];

app.get(‘/‘, function(req, res) {
    res.render(‘home‘);
});

app.get(‘/about‘, function(req, res) {
    var randomFortune = 
        fortunes[Math.floor(Math.random() * fortunes.length)];
    res.render(‘about‘, {fortune: randomFortune});
});


app.use(function(req, res) {
    res.status(404);
    res.render(‘404‘);
});

app.use(function(err, req, res, next) {
    console.error(err.stack);
    res.status(500);
    res.render(‘500‘);
});

app.listen(app.get(‘port‘), function() {
    console.log(‘Express started on http://localhost: ‘ + 
        app.get(‘port‘) + ‘; press Ctrl-C teminate.‘);
});

书和输出:

 

技术分享

技术分享

EXPRESS.JS再出发

标签:

原文地址:http://www.cnblogs.com/aguncn/p/5173942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!