使用Node+Express 建構網站(二)

第六章 請求與回應物件

URL的各部分

http://localhost:3000/about?test=1#history
https://google.com/#q=express

協定主機名稱連接埠路徑查詢字串片段
http://
https://
localhost
google.com
:3000

/about
/
?test=1#history
#q=express
片段:
片段(fragment)(或雜湊 (hash)),不會傳至伺服器,它完全是供瀏覽器使用的。單頁應用程式或重度的AJAX應用程式已經越來越喜歡使用片段來控制應用程式。

使用Node+Express 建構網站(一)

第三章

安裝Express

使用Express來節省時間

npm install  --save express

寫一個簡單的程式

var express = require('express');
var app = express();
app.set('port', process.env.PORT || 3000);
app.get('/', function(req, res) {
res.type('text/plain');
res.send('Bake Node Book');
});
app.get('/about', function(req, res) {
res.type('text/plain');
res.send('About my Bake Node Book');
});
app.use(function(req, res){
res.type('text/plain');
res.status(404);
res.send('404 - Not Found');
});
app.use(function(err, req, res, next){
console.error(err.stack);
res.type('test/plain');
res.status(500);
res.send('500 - Server Error');
});
app.listen(app.get('port'), function(){
console.log('Express started on http://localhost:');
app.get(('port') + ';press Ctrl-C to terminate.');
});
繼續閱讀 “使用Node+Express 建構網站(一)”