各位前輩好:
我寫了一段node程式如下:
var http = require('http');
var server = http.createServer(function (req, res) {
switch (req.url) {
case '/':
res.writeHead(200, { 'Content-Type': 'text-html' });
res.write('<html><body>This is Home Page !</body></html>');
console.log('This is Home Page ');
res.end();
break;
case '/student':
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<html><body>This is Student Page !</body></html>');
console.log('This is Student Page !');
res.end();
break;
case '/admin':
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<html><body>This is Admin Page !</body></html>');
console.log('This is Admin Page !');
res.end();
break;
default:
console.log('This is an invalid req !');
res.end('This is an invalid request !');
break;
}
});
server.listen(5000);
當在browser輸入localhost:5000/student 在server端出現:
This is Student Page!
This is an invalid req !
想請問為何會執行default裏的程式? 謝謝.
在 switch 前加上
console.log(req.url,'url');
看看是不是發送了兩次。