Skip to content

Commit 0292d3e

Browse files
author
lucifer
committed
first commit
0 parents  commit 0292d3e

File tree

10 files changed

+260
-0
lines changed

10 files changed

+260
-0
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
/node_modules

Diff for: app.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const Koa = require("koa");
2+
const app = new Koa();
3+
const cors = require("@koa/cors");
4+
const views = require("koa-views");
5+
const json = require("koa-json");
6+
const onerror = require("koa-onerror");
7+
const bodyparser = require("koa-bodyparser");
8+
const logger = require("koa-logger");
9+
10+
const index = require("./routes/index");
11+
const users = require("./routes/users");
12+
13+
// error handler
14+
onerror(app);
15+
16+
// middlewares
17+
app.use(
18+
bodyparser({
19+
enableTypes: ["json", "form", "text"],
20+
})
21+
);
22+
app.use(cors());
23+
app.use(json());
24+
app.use(logger());
25+
app.use(require("koa-static")(__dirname + "/public"));
26+
27+
app.use(
28+
views(__dirname + "/views", {
29+
extension: "pug",
30+
})
31+
);
32+
33+
// logger
34+
app.use(async (ctx, next) => {
35+
const start = new Date();
36+
await next();
37+
const ms = new Date() - start;
38+
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
39+
});
40+
41+
// routes
42+
app.use(index.routes(), index.allowedMethods());
43+
app.use(users.routes(), users.allowedMethods());
44+
45+
// error-handling
46+
app.on("error", (err, ctx) => {
47+
console.error("server error", err, ctx);
48+
});
49+
50+
module.exports = app;

Diff for: bin/www

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var app = require('../app');
8+
var debug = require('debug')('demo:server');
9+
var http = require('http');
10+
11+
/**
12+
* Get port from environment and store in Express.
13+
*/
14+
15+
var port = normalizePort(process.env.PORT || '3000');
16+
// app.set('port', port);
17+
18+
/**
19+
* Create HTTP server.
20+
*/
21+
22+
var server = http.createServer(app.callback());
23+
24+
/**
25+
* Listen on provided port, on all network interfaces.
26+
*/
27+
28+
server.listen(port);
29+
server.on('error', onError);
30+
server.on('listening', onListening);
31+
32+
/**
33+
* Normalize a port into a number, string, or false.
34+
*/
35+
36+
function normalizePort(val) {
37+
var port = parseInt(val, 10);
38+
39+
if (isNaN(port)) {
40+
// named pipe
41+
return val;
42+
}
43+
44+
if (port >= 0) {
45+
// port number
46+
return port;
47+
}
48+
49+
return false;
50+
}
51+
52+
/**
53+
* Event listener for HTTP server "error" event.
54+
*/
55+
56+
function onError(error) {
57+
if (error.syscall !== 'listen') {
58+
throw error;
59+
}
60+
61+
var bind = typeof port === 'string'
62+
? 'Pipe ' + port
63+
: 'Port ' + port;
64+
65+
// handle specific listen errors with friendly messages
66+
switch (error.code) {
67+
case 'EACCES':
68+
console.error(bind + ' requires elevated privileges');
69+
process.exit(1);
70+
break;
71+
case 'EADDRINUSE':
72+
console.error(bind + ' is already in use');
73+
process.exit(1);
74+
break;
75+
default:
76+
throw error;
77+
}
78+
}
79+
80+
/**
81+
* Event listener for HTTP server "listening" event.
82+
*/
83+
84+
function onListening() {
85+
var addr = server.address();
86+
var bind = typeof addr === 'string'
87+
? 'pipe ' + addr
88+
: 'port ' + addr.port;
89+
debug('Listening on ' + bind);
90+
}

Diff for: package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "leetcode-pp-node",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"start": "node bin/www",
7+
"dev": "./node_modules/.bin/nodemon bin/www",
8+
"prd": "pm2 start bin/www",
9+
"test": "echo \"Error: no test specified\" && exit 1"
10+
},
11+
"dependencies": {
12+
"@koa/cors": "^3.1.0",
13+
"debug": "^4.1.1",
14+
"koa": "^2.7.0",
15+
"koa-bodyparser": "^4.2.1",
16+
"koa-convert": "^1.2.0",
17+
"koa-json": "^2.0.2",
18+
"koa-logger": "^3.2.0",
19+
"koa-onerror": "^4.1.0",
20+
"koa-router": "^7.4.0",
21+
"koa-static": "^5.0.0",
22+
"koa-views": "^6.2.0",
23+
"node-fetch": "^2.6.1",
24+
"pug": "^2.0.3"
25+
},
26+
"devDependencies": {
27+
"nodemon": "^1.19.1"
28+
}
29+
}

Diff for: public/stylesheets/style.css

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
body {
2+
padding: 50px;
3+
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4+
}
5+
6+
a {
7+
color: #00B7FF;
8+
}

Diff for: routes/index.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const fetch = require("node-fetch");
2+
const router = require("koa-router")();
3+
4+
const clientId = "c16b80e7b58a5a007157";
5+
const clientSecret = process.env.secret;
6+
7+
const db = [
8+
{
9+
login: "azl397985856",
10+
},
11+
];
12+
13+
router.get("/", async (ctx) => {
14+
await ctx.render("index", {
15+
title: "欢迎来到 91 天学算法~",
16+
});
17+
});
18+
19+
router.get("/api/v1/user", async (ctx) => {
20+
const code = ctx.query.code;
21+
const { access_token } = await fetch(
22+
`https://github.com/login/oauth/access_token?code=${code}&client_id=${clientId}&client_secret=${clientSecret}`,
23+
{
24+
method: "POST",
25+
headers: {
26+
Accept: "application/json",
27+
},
28+
}
29+
).then((res) => res.json());
30+
31+
const user = await fetch("https://api.github.com/user", {
32+
headers: {
33+
Accept: "application/json",
34+
Authorization: `token ${access_token}`,
35+
},
36+
}).then((res) => res.json());
37+
if (db.find((q) => q.login === login)) {
38+
ctx.body = {
39+
...user,
40+
pay: true,
41+
};
42+
} else {
43+
ctx.body = {
44+
...user,
45+
pay: false,
46+
};
47+
}
48+
});
49+
50+
module.exports = router;

Diff for: routes/users.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const router = require('koa-router')()
2+
3+
router.prefix('/users')
4+
5+
router.get('/', function (ctx, next) {
6+
ctx.body = 'this is a users response!'
7+
})
8+
9+
router.get('/bar', function (ctx, next) {
10+
ctx.body = 'this is a users/bar response'
11+
})
12+
13+
module.exports = router

Diff for: views/error.pug

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extends layout
2+
3+
block content
4+
h1= message
5+
h2= error.status
6+
pre #{error.stack}

Diff for: views/index.pug

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extends layout
2+
3+
block content
4+
h1= title
5+
p Welcome to #{title}

Diff for: views/layout.pug

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
doctype html
2+
html
3+
head
4+
title= title
5+
link(rel='stylesheet', href='/stylesheets/style.css')
6+
body
7+
block content

0 commit comments

Comments
 (0)