http://nodejs.cn/api/http.html node html模块api
    http://nodejs.cn/api/fs.html node fs模块api

    目录/
    index.js
    home.html
    about.html
    notfound.html
    pubilc/ // 静态资源文件路径
    img/
    logo.jpg

    1. <!DOCTYPE HTML>
    2. <html lang="zh-cn">
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html;charset:utf-8;" />
    5. <title>homepage</title>
    6. <head>
    7. <body>
    8. <img href="./img/logo.jpg" alt="logo"/>
    9. </body>
    10. </html>
    1. <!DOCTYPE HTML>
    2. <html lang="zh-cn">
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html;charset:utf-8;" />
    5. <title>about</title>
    6. <head>
    7. <body></body>
    8. </html>
    1. <!DOCTYPE HTML>
    2. <html lang="zh-cn">
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html;charset:utf-8;" />
    5. <title>not found</title>
    6. <head>
    7. <body>404</body>
    8. </html>
    1. <!DOCTYPE HTML>
    2. <html lang="zh-cn">
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html;charset:utf-8;" />
    5. <title>about</title>
    6. <head>
    7. <body></body>
    8. </html>
    1. // index.js
    2. /** 第一版代码
    3. var http = require('http');
    4. http.createServer(function(req, res){
    5. res.writeHead(200, {'Content-Type':'text/plain'});
    6. res.end('hello world!');
    7. }).listen(3000);
    8. console.log('Server started on localhost:3000; press Ctrl+c to terminate...');
    9. */
    10. /** 第二版代码
    11. var http = require('http');
    12. http.createServer(function(req, res){
    13. var path = req.url.replace(/\?(?:\?.*)?$/, '').toLowerCase();
    14. switch(path) {
    15. case '':
    16. res.writeHead(200, {'Content-Type':'text/plain'});
    17. res.end('Homepage!');
    18. break;
    19. case '/about':
    20. res.writeHead(200, {'Content-Type':'text/plain'});
    21. res.end('about!');
    22. break;
    23. default:
    24. res.writeHead(404, {'Content-Type':'text/plain'});
    25. res.end('Not Found!');
    26. break;
    27. }
    28. }).listen(3000);
    29. console.log('Server started on localhost:3000; press Ctrl+c to terminate...');
    30. */
    31. var http = require('http'),
    32. fs = require('fs');
    33. function serverStaticFile(res, path, contentType, responseCode) {
    34. if(!responseCode) responseCode = 200;
    35. fs.readFile(__dirname + path, function(err, data){
    36. if (err) {
    37. res.writehead(500, {'Content-Type':'text/plain'});
    38. res.end('500 - Internal Error');
    39. } else {
    40. res.writeHead(responseCode, { 'Content-Type': contentType });
    41. res.end(data);
    42. }
    43. });
    44. }
    45. http.createServer(function(req, res){
    46. var path = req.url.replace(/\?(?:\?.*)?$/, '').toLowerCase();
    47. switch(path) {
    48. case '':
    49. serverStaticFile(res, '/home.html', 'text/html');
    50. break;
    51. case '/about':
    52. serverStaticFile(res, '/about.html', 'text/html');
    53. break;
    54. case '/img/logo.jpg':
    55. serverStaticFile(res, '/img/logo.jpg', 'image/jpeg');
    56. break;
    57. default:
    58. serverStaticFile(res, '/notfound.html', 'text/html', 404);
    59. break;
    60. }
    61. }).listen(3000);
    62. console.log('Server started on localhost:3000; press Ctrl+c to terminate...');