created backend
This commit is contained in:
1 parent
4f4f0bb8e3
commit
6d02516ee9
1 file changed
+47
@@ -0,0 +1,47 @@
|
||||
// server.js
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const routes = require('./routes/routes.js');
|
||||
const sequelize = require('./db/database.js');
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 4000;
|
||||
|
||||
// Middleware
|
||||
app.use(bodyParser.json()); // for parsing application/json
|
||||
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
|
||||
|
||||
// CORS Middleware to allow requests from any origin
|
||||
app.use((req, res, next) => {
|
||||
res.header('Access-Control-Allow-Origin', '*');
|
||||
res.header(
|
||||
'Access-Control-Allow-Headers',
|
||||
'Origin, X-Requested-With, Content-Type, Accept, Authorization'
|
||||
);
|
||||
if (req.method === 'OPTIONS') {
|
||||
res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
|
||||
return res.status(200).json({});
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
// Routes
|
||||
app.use('/api/v1', routes);
|
||||
|
||||
// Start the server
|
||||
const relations = require('./db/relations.js')
|
||||
|
||||
sequelize
|
||||
.sync({force: true})
|
||||
.then((result) => {
|
||||
// console.log(result);
|
||||
app.listen(PORT, () => {
|
||||
const Entry = require("./models/Entry.js")
|
||||
Entry.create({title: "Avengers Endgame"})
|
||||
console.log(`Server running on http://localhost:${PORT}`);
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
console.log("error: ____________")
|
||||
console.log(err)
|
||||
});
|
||||
Reference in new issue
Block a user