-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
45 lines (35 loc) · 997 Bytes
/
index.js
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
/*
* Copyright 2022 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
// This is the file which uses our web framework.
const { SimpleFramework } = require('./simple-framework')
const { authenticate } = require('./lib/authenticate')
const getUsers = () => {
const users = [
{ id: 1, username: 'user1', email: '[email protected]' },
{ id: 2, username: 'user2', email: '[email protected]' },
{ id: 3, username: 'user3', email: '[email protected]' }
];
return JSON.stringify(users);
}
let server = new SimpleFramework()
server.all(function authenticateMiddleware(req, res) {
if (authenticate()) {
console.log("Authenticated!")
} else {
res.statusCode = 403
res.end()
}
})
server.get('/api/users', function(req, res) {
res.write(getUsers())
res.end()
})
server.get('/home', function(req, res) {
const renderedView = server.render('home')
res.write(renderedView)
res.end()
})
server.start(3000)