-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (36 loc) · 1.12 KB
/
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
const express = require('express')
const Sequelize = require('sequelize')
const sequelize = new Sequelize('postgres://postgres:admin@localhost:5432/postgres')
const app = express()
const cors = require('cors')
app.use(cors())
const port = 8081
app.use(express.json());
sequelize.authenticate().then(() => {
console.log('Connection has been established successfully.');
}).catch(err => {
console.error('Unable to connect to the database:', err);
});
const User = sequelize.define('user', {
firstName: {
type: Sequelize.STRING,
allowNull: false
},
lastName: {
type: Sequelize.STRING
}
}, {});
User.sync({ force: true })
app.get('/', (req, res) => res.json({ message: 'test api' }))
app.post('/createUser', async(req, res) => {
try {
console.log('data:', req.body)
const newUser = new User(req.body)
await newUser.save()
res.json({ user: newUser }) // Returns the new user that is created in the database
} catch (error) {
console.error(error)
res.end("error");
}
})
app.listen(port, () => console.log(`Ejecutando api en puerto ${port}`))