-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (52 loc) · 1.92 KB
/
Copy pathserver.js
File metadata and controls
64 lines (52 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require('dotenv').config();
const express = require('express');
const path = require('path');
const app = express();
// Build injects PORT at runtime; the web process must listen on it.
const PORT = process.env.PORT || 3000;
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
const startedAt = new Date();
// ---------------------------------------------------------------------------
// Pages
// ---------------------------------------------------------------------------
app.get('/', (req, res) => {
res.render('index', {
greeting: process.env.GREETING || 'Hello from Build!',
env: {
deployedAt: startedAt.toISOString(),
nodeVersion: process.version,
port: PORT,
uptime: Math.floor(process.uptime()),
},
});
});
// ---------------------------------------------------------------------------
// API — a tiny JSON endpoint to demo with curl or fetch()
// ---------------------------------------------------------------------------
app.get('/api/status', (req, res) => {
res.json({
status: 'ok',
uptimeSeconds: Math.floor(process.uptime()),
startedAt: startedAt.toISOString(),
node: process.version,
});
});
app.get('/api/greeting', (req, res) => {
res.json({
// Change GREETING in your app's config vars and watch this update —
// no redeploy needed: bld config:set GREETING="Hi team" -a <your-app>
greeting: process.env.GREETING || 'Hello from Build!',
});
});
// ---------------------------------------------------------------------------
// Try it: add your own route here, push to GitHub, and redeploy.
// ---------------------------------------------------------------------------
// app.get('/yourname', (req, res) => {
// res.send('This page was added by <yourname>!');
// });
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});