Skip to content

Commit cbfb7fc

Browse files
committed
Added upgraded hello world visuals
1 parent 9c342d3 commit cbfb7fc

File tree

12 files changed

+2711
-22
lines changed

12 files changed

+2711
-22
lines changed

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
# Express Hello World
1+
# Cyclic Hello World
22

3-
Implements the Expressjs Hello World example to provide a faster start deploying onto the Cyclic platform
3+
This is a basic Expressjs app with static file hosting and
44

55
## Installation
66

7-
- Fork this repo
7+
- Fork this repo (https://github.com/cyclic-software/express-hello-world)
88
- Clone to your local
99
- `npm install`
1010

11-
## Running
11+
## Local
1212

13-
- `npm start`
14-
- Browser: `http://localhost:3443/some/path?q=query+one&q=second+query&single=value`
15-
- Commandline: `curl -i -XGET "http://localhost:3443/cmd/line-curl"`
13+
- `npm run serve`
14+
- Browser: `http://localhost:3000/some/path?q=query+one&q=second+query&single=value`
15+
- Commandline: `curl -i -XGET "http://localhost:3000/cmd/line-curl"`
1616

1717
## Cyclic Runtime
1818

19-
- The Cyclic runtime expects a file in the root of your project named `app.js`
20-
- The runtime will `const app = require('./app')`
21-
- The runtime expects the express app to be exported as: `module.exports = app`
19+
- The Cyclic runtime expects a file in the root of your project named `server.js`
20+
- The runtime will `node .` which runs your `server.js` by default.

app.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,55 @@
11
const express = require('express')
2+
const path = require("path");
23
const app = express()
3-
const fs = require('fs')
44

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+
});
611

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 '/'
731
app.get('/', (req, res) => {
8-
console.log('[hello-world] root handler called')
32+
console.log('[express-hello-world] root handler called')
933
res
1034
.set('x-powered-by', 'cyclic.sh')
1135
.send('<h1>Hello World!</h1>')
1236
.end()
1337
})
1438

1539
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}`)
2141
res
2242
.set('x-powered-by', 'cyclic.sh')
2343
.json({
24-
msg: "Not strickly part of the hello world but you get the picture.",
25-
version,
2644
at: new Date().toISOString(),
2745
method: req.method,
2846
hostname: req.hostname,
2947
ip: req.ip,
30-
path: req.params[0],
3148
query: req.query,
3249
headers: req.headers,
3350
cookies: req.cookies,
51+
params: req.params,
52+
env: process.env
3453
})
3554
.end()
3655
})

0 commit comments

Comments
 (0)