-
Notifications
You must be signed in to change notification settings - Fork 0
/
express-test.js
102 lines (95 loc) · 3.2 KB
/
express-test.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
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
100
101
102
// ssh -i "device-manager.pem" [email protected]
// ssh -i "device-manager.pem" [email protected]
// mysql -h cordeirolucas42.cxpqrbghq6gw.sa-east-1.rds.amazonaws.com -P 3306 -u admin -p
// sudo "$(which node)" express-test.js
// https://medium.com/cs-note/setup-node-and-express-on-aws-ec2-windows-7-8cb499ab14eb
// Using pm2 to handle the application https://www.npmjs.com/package/pm2
// CONFIGURE DATABASE
require('dotenv').config(); //process.env. to use enviroment variables
const { Sequelize, DataTypes, Model } = require('sequelize');
const sequelize = new Sequelize('devices_manager', 'admin', process.env.DB_PASS, {
dialect: 'mysql',
host: process.env.DB_ENDPOINT,
port: process.env.DB_PORT
});
class Category extends Model { }
class Device extends Model { }
Category.init({
id: {
type: DataTypes.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true,
unique: true
},
name: {
type: DataTypes.STRING(128),
allowNull: false
}
}, {
// Other model options go here
sequelize, // We need to pass the connection instance
modelName: 'Category' // We need to choose the model name
});
Device.init({
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
unique: true
},
color: {
type: DataTypes.STRING(16),
allowNull: false,
validate: {
isAlpha: true
}
},
partNumber: {
type: DataTypes.INTEGER,
validate: {
isInt: true,
min: 0
}
}
}, {
// Other model options go here
sequelize, // We need to pass the connection instance
modelName: 'Device' // We need to choose the model name
});
Category.hasMany(Device, {foreignKey: "category"})
Device.belongsTo(Category)
// CONFIGURE EXPRESS APP
const express = require('express')
const app = express()
const port = 9000
// API ENDPOINTS
app.get('/', (req, res) => {
res.send(`Olá! Bem-vindo!`)
})
app.get('/category/:id', async (req, res) => {
if (req.params.id) {
const category = await Category.findByPk(req.params.id)
if (category) res.send(`Categoria com id ${category.id}, e nome ${category.name}.`)
} else {
res.send(`Category not found`)
}
})
app.get('/device/:id', async (req, res) => {
if (req.params.id) {
const device = await Device.findByPk(req.params.id, {include: Category})
if (device) res.send(`Device with id ${device.id} from category ${device.Category.name}, color ${device.color} and part number ${device.partNumber}.`)
/* const device = await Device.findByPk(req.params.id)
if (device) {
const category = await Category.findByPk(device.category)
if (category) {
res.send(`Device with id ${device.id} from category ${category.name}, color ${device.color} and part number ${device.partNumber}.`)
}
} */
} else {
res.send(`Device not found`)
}
})
app.listen(port, () => {
console.log(`Example app listening at http://ec2-54-94-254-219.sa-east-1.compute.amazonaws.com:${port}`)
})