-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-server.js
More file actions
58 lines (48 loc) · 1.33 KB
/
Copy pathexample-server.js
File metadata and controls
58 lines (48 loc) · 1.33 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
//Copied from the documentation: https://github.com/axiand/origami-docs/blob/master/Getting%20Started/Intro.md
// Import Origami
const { Origami } = require('./index')
// Create an app running on port 3000
var app = new Origami(3000)
// Fire up the server
app.listen((app) => {
console.log(`Server running on port ${app.port} - http://localhost:${app.port}/`)
})
// Create a GET /helloworld route
app.Route('GET', '/helloworld', (ctx, res) => {
return res
.write(
{'greeting': 'Hello, World!'}
)
})
// Create a route with a variable
// Origami calls these "includes"
app.Route('GET', '/articles/:artId', (ctx, res) => {
let article = ctx.includes.artId
/** Your database logic would probably go here. **/
return res.write(
{
'id': article.key,
'author': 'Firstname Lastname',
'title': 'Article Name',
'body': 'Article Body'
}
)
})
// Create a component class
class UserClass {
constructor() {
return {
Get: (key) => {
return {"id": key}
}
}
}
}
// Bind our class to the app
app.Component("User", UserClass)
// Create a route that uses the new class
app.Route('GET', '/users/:User target', (ctx, res) => {
return {
"user": ctx.bake("target")
}
})