oxedyne/fe2o3/fe2o3_text/tests/detect_corpus/js/node_require.txt
1.2 KiB, 1 run
created by r1870400018:12004, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | const http = require('http'); |
| 2 | const url = require('url'); |
| 3 | const crypto = require('crypto'); |
| 4 | |
| 5 | const PORT = process.env.PORT || 3000; |
| 6 | |
| 7 | function generateToken() { |
| 8 | return crypto.randomBytes(32).toString('hex'); |
| 9 | } |
| 10 | |
| 11 | const sessions = new Map(); |
| 12 | |
| 13 | const server = http.createServer((req, res) => { |
| 14 | const parsed = url.parse(req.url, true); |
| 15 | |
| 16 | if (parsed.pathname === '/login' && req.method === 'POST') { |
| 17 | const token = generateToken(); |
| 18 | sessions.set(token, { created: Date.now() }); |
| 19 | res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 20 | res.end(JSON.stringify({ token })); |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | if (parsed.pathname === '/status') { |
| 25 | const auth = req.headers['authorization']; |
| 26 | if (!auth || !sessions.has(auth)) { |
| 27 | res.writeHead(401); |
| 28 | res.end('unauthorized'); |
| 29 | return; |
| 30 | } |
| 31 | res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 32 | res.end(JSON.stringify({ |
| 33 | sessions: sessions.size, |
| 34 | uptime: process.uptime(), |
| 35 | })); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | res.writeHead(404); |
| 40 | res.end('not found'); |
| 41 | }); |
| 42 | |
| 43 | server.listen(PORT, () => { |
| 44 | console.log(`listening on port ${PORT}`); |
| 45 | }); |