-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
99 lines (83 loc) · 2.44 KB
/
app.js
File metadata and controls
99 lines (83 loc) · 2.44 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// ========== server.js ==============
// Requirements
const mongoose = require('mongoose')
const express = require('express')
const bodyParser = require('body-parser')
const AdminBro = require('admin-bro')
const AdminBroExpressjs = require('admin-bro-expressjs')
// We have to tell AdminBro that we will manage mongoose resources with it
AdminBro.registerAdapter(require('admin-bro-mongoose'))
// express server definition
const app = express()
app.use(bodyParser.json())
// Resources definitions
const User = mongoose.model('User', { name: String, email: String, surname: String })
var artcileSchema = new mongoose.Schema({
title: String,
body: String,
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
created_at: { type: Date, default: Date.now }
});
const Article = mongoose.model('Article', artcileSchema);
// Routes definitions
app.get('/', (req, res) => res.send('Hello World!'))
// Route which returns last 100 users from the database
app.get('/users', async (req, res) => {
const users = await User.find({}).limit(10)
res.send(users)
})
// Route which creates new user
app.post('/users', async (req, res) => {
const user = await new User(req.body.user).save()
res.send(user)
})
// Route whick retuns articles
app.get('/articles', async (req, res) => {
const articles = await Article.find({}).limit(10)
res.send(articles)
})
const createParent = {
name: 'Create',
icon: 'fa fa-coffee',
}
const managerParent = {
name: 'Manage',
icon: 'fa fa-cog',
}
// Pass all configuration settings to AdminBro
const adminBro = new AdminBro({
resources: [
{
resource: User, options: { parent: managerParent }
},
{
resource: Article, options: {
properties: {
body: { type: 'richtext' },
created_at: { isVisible: { list: false, filter: false, show: true, edit: false } }
},
parent: createParent
}
}
],
rootPath: '/admin',
branding: {
companyName: 'Escola de Javascript',
},
dashboard: {
component: AdminBro.require('./dashboard')
},
})
// Build and use a router which will handle all AdminBro routes
const router = AdminBroExpressjs.buildRouter(adminBro)
app.use(adminBro.options.rootPath, router)
// Running the server
const run = async () => {
await mongoose.connect('mongodb://localhost/admin', { useNewUrlParser: true })
await app.listen(8080, () => console.log(`Example app listening on port 8080!`))
}
run()