|
1 | 1 | const express = require('express') |
| 2 | +const path = require("path"); |
2 | 3 | const app = express() |
3 | | -const fs = require('fs') |
4 | 4 |
|
5 | | -const VERSION_FILENAME='./.git/refs/heads/main' |
| 5 | +// ///////////////////////////////////////////////////////////////////////////// |
| 6 | +// Logs all requests path and method |
| 7 | +app.use(function (req, res, next) { |
| 8 | + console.log(`[${new Date().toISOString()}] ${req.ip} ${req.method} ${req.path}`); |
| 9 | + next(); |
| 10 | +}); |
6 | 11 |
|
| 12 | +// ///////////////////////////////////////////////////////////////////////////// |
| 13 | +// This configures static hosting for files in /public that have the extensions |
| 14 | +// listed in the array. |
| 15 | +var options = { |
| 16 | + dotfiles: 'ignore', |
| 17 | + etag: false, |
| 18 | + extensions: ['htm', 'html','css','js','ico','jpg','jpeg','png','svg'], |
| 19 | + index: ['index.html'], |
| 20 | + maxAge: '1m', |
| 21 | + redirect: false, |
| 22 | + setHeaders: function (res, path, stat) { |
| 23 | + res.set('x-timestamp', Date.now()) |
| 24 | + } |
| 25 | +} |
| 26 | +app.use(express.static('public', options)) |
| 27 | + |
| 28 | + |
| 29 | +// ///////////////////////////////////////////////////////////////////////////// |
| 30 | +// This handles GET requests to the root route '/' |
7 | 31 | app.get('/', (req, res) => { |
8 | | - console.log('[hello-world] root handler called') |
| 32 | + console.log('[express-hello-world] root handler called') |
9 | 33 | res |
10 | 34 | .set('x-powered-by', 'cyclic.sh') |
11 | 35 | .send('<h1>Hello World!</h1>') |
12 | 36 | .end() |
13 | 37 | }) |
14 | 38 |
|
15 | 39 | app.use('*', (req,res) => { |
16 | | - console.log('[hello-world] Star handler called') |
17 | | - let version = 'unknown' |
18 | | - if (fs.existsSync(VERSION_FILENAME)) { |
19 | | - version = fs.readFileSync(VERSION_FILENAME).toString().substr(0,8) |
20 | | - } |
| 40 | + // console.log(`[express-hello-world] * handler ${req.method}:${req.path}`) |
21 | 41 | res |
22 | 42 | .set('x-powered-by', 'cyclic.sh') |
23 | 43 | .json({ |
24 | | - msg: "Not strickly part of the hello world but you get the picture.", |
25 | | - version, |
26 | 44 | at: new Date().toISOString(), |
27 | 45 | method: req.method, |
28 | 46 | hostname: req.hostname, |
29 | 47 | ip: req.ip, |
30 | | - path: req.params[0], |
31 | 48 | query: req.query, |
32 | 49 | headers: req.headers, |
33 | 50 | cookies: req.cookies, |
| 51 | + params: req.params, |
| 52 | + env: process.env |
34 | 53 | }) |
35 | 54 | .end() |
36 | 55 | }) |
|
0 commit comments