Slog4_使用后端框架KOA实现静态web服务器

2018-08-21  本文已影响12人  ArthurSlog_
我的微信公众号

梦想是锁不住的

开发环境MacOS(High Sierra 10.13.5)

cd ~/Desktop

mkdir node_koa_learningload

cd node_koa_learningload

npm init

sudo npm install koa koa-static

index.js

const serve = require('koa-static');
const Koa = require('koa');
const app = new Koa();

// $ GET /package.json
app.use(serve('.'));

// $ GET /hello.txt
app.use(serve('test/fixtures'));

// or use absolute paths
app.use(serve(__dirname + '/test/fixtures'));

app.listen(3000);

console.log('listening on port 3000');

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ArthurSlog</title>
</head>
<body>

<h1>The static web server by ArthurSlog</h1>

</body>
</html>
  1. 根据node工程的配置文件package.json里指定的入口点“main”决定路由
// $ GET /package.json
app.use(serve('.'));
  1. 使用相对路径作为路由,默认的路由文件由package.json里的入口点“main”决定
// $ GET /hello.txt
app.use(serve('test/fixtures'));
  1. 使用绝对路径作为路由,默认的路由文件由package.json里的入口点“main”决定
// or use absolute paths
app.use(serve(__dirname + '/test/fixtures'));

index.js

const serve = require('koa-static');
const Koa = require('koa');
const app = new Koa();

// $ GET /package.json
app.use(serve('.'));

app.listen(3000);

console.log('listening on port 3000');

node index.js

欢迎关注我的微信公众号 ArthurSlog

我的微信公众号

如果你喜欢我的文章 欢迎点赞 留言

谢谢

上一篇下一篇

猜你喜欢

热点阅读