webserver: html && publc

静态server

  • apache(LAMP之a)
  • nginx(LNMP之n)

每个http server framework里都内置的,虽然以后不会用

Node.js版本的静态server

nodejs里可以使用更简单的办法完成这事儿

  1. npm i -g http-server
  2. http-server . -p 8080 -o

使用Node.js实现http还是非常简单合适的。

express static

  1. var express = require('express');
  2. var app = express();
  3. var path = require('path');
  4. var open = require("open");
  5. app.use(express.static(path.join(__dirname, 'www/')));
  6. app.get('/', function (req, res) {
  7. res.send('Hello World')
  8. })
  9. // 随机端口3000 - 10000 之间
  10. app.listen(4001)
  11. open("http://127.0.0.1:4001");

serve-static

基于connect的中间件

代码

  1. var finalhandler = require('finalhandler')
  2. var http = require('http')
  3. var serveStatic = require('serve-static')
  4. // Serve up public/ftp folder
  5. var serve = serveStatic('public', {'index': ['index.html', 'index.htm']})
  6. // Create server
  7. var server = http.createServer(function onRequest (req, res) {
  8. serve(req, res, finalhandler(req, res))
  9. })
  10. // Listen
  11. server.listen(3000)
  • 安装依赖finalhandler && serve-static
  • 创建public目录
  • 创建index.html
  • 启动这个静态服务器

serve-index

基于connect的中间件

  1. var finalhandler = require('finalhandler')
  2. var http = require('http')
  3. var serveIndex = require('serve-index')
  4. var serveStatic = require('serve-static')
  5. // Serve directory indexes for public/stylesheets folder (with icons)
  6. var index = serveIndex('public/', {'icons': true})
  7. // Serve up public/ftp folder files
  8. var serve = serveStatic('public')
  9. // Create server
  10. var server = http.createServer(function onRequest(req, res){
  11. var done = finalhandler(req, res)
  12. serve(req, res, function onNext(err) {
  13. if (err) return done(err)
  14. index(req, res, done)
  15. })
  16. })
  17. // Listen
  18. server.listen(3002)

Static Index

实现一个简单的网页

  • 创建html
  • 创建css和js和images目录
  • 完成hello world

open

  1. var express = require('express');
  2. var app = express();
  3. var path = require('path');
  4. var open = require("open");
  5. app.get('/', function (req, res) {
  6. res.send('Hello World')
  7. })
  8. // 随机端口3000 - 10000 之间
  9. app.listen(4001)
  10. open("http://127.0.0.1:4001");