NodeJs學習筆記

由於Express3.0以上的版本不支持layout了,因此,NodeJs開發指南的blog代碼不能用,可是能夠經過下面的方法實現css

npm install express-partialshtml

partials=require('express-partials');
app.use(partials());
web

 

這樣就能夠使用layout了express

詳細代碼以下
app.js
-------------------------------------------------------npm

/**
* Module dependencies.
*/
 
var express =require('express')
  , routes =require('./routes')
  , user =require('./routes/user')
  , http =require('http')
  ,partials =require('express-partials')
  , path =require('path');
 
var app = express();
 
app.configure(function(){
  app.set('port', process.env.PORT ||3000);
  app.set('views', __dirname +'/views');
  app.set('view engine','ejs');
  app.use(partials());
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser('your secret here'));
  app.use(express.session());
  app.use(app.router);
  app.use(express.static(path.join(__dirname,'public')));
});
 
app.configure('development',function(){
  app.use(express.errorHandler());
});
/*
開始微博網站的製做。
路由部分
*/
app.get("/",routes.index);
app.get("/u/:user",routes.user);
app.post("/post",routes.post);
app.get("/reg",routes.reg);
app.post("/reg",routes.doReg);
app.get("/login",routes.login);
app.post("/login",routes.doLogin);
app.get("/logout",routes.logout);
/*
路由結束
*/
http.createServer(app).listen(app.get('port'),function(){
  console.log("Express server listening on port "+ app.get('port'));
});cookie


---------------------------------------------------------------------------------
routes/index.js
--------------------------------------------------------------------------------session

 
/*
* GET home page.
*/
 
exports.index =function(req, res){
  res.render('index.ejs',{ title:'Express'});
};
exports.hello =function(req,res){
    res.send("this time is"+newDate().toString());
}
//微博路由開啓
exports.user=function(req,res){
};
exports.post=function(req,res){
};
exports.reg=function(req,res){
};
exports.doReg=function(req,res){
};
exports.login=function(req,res){
};
exports.doLogin=function(req,res){
};
exports.logout=function(req,res){
};
//微博路由結束app


--------------------------------------------------------------
views/layout.ejs
--------------------------------------------------------------ide

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <linkrel='stylesheet'href='/stylesheets/style.css'/>
  </head>
  <body>
    <%- body %>
  </body>
</html>post


---------------------------------------------------------------
views/index.ejs
---------------------------------------------------------------

  <h1><%= title %></h1>
    <p>Welcome to <%= title %> this is a test</p>
相關文章
相關標籤/搜索