-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
1,441 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/node_modules | ||
/node_modules | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
const express = require('express'); | ||
const app = express(); | ||
const port = 3000; | ||
// Importing routes | ||
const dotenv = require('dotenv'); | ||
const cors = require('cors'); | ||
const CategoriesRouter = require('./routes/catagories'); | ||
const morgan = require('morgan') | ||
const morgan = require('morgan'); | ||
|
||
// Middleware | ||
app.use(express.json()); | ||
dotenv.config(); | ||
|
||
// Morgan | ||
app.use(morgan('dev')); | ||
// Middleware | ||
app.use(express.json()); // Parse JSON bodies | ||
app.use(morgan('dev')); // Logging middleware | ||
app.use(cors()); // Enable CORS | ||
|
||
// Routing | ||
app.use('/api/v1/categories', CategoriesRouter); | ||
|
||
// Start server | ||
// Server | ||
const port = process.env.PORT || 3000; // Use port from environment variable or default to 3000 | ||
app.listen(port, () => { | ||
console.log(`Server berjalan di port ${port}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require('dotenv').config(); | ||
|
||
module.exports = { | ||
development: { | ||
username: process.env.DB_USERNAME, | ||
password: process.env.DB_PASSWORD, | ||
database: process.env.DB_DATABASE, | ||
host: process.env.DB_HOST, | ||
dialect: process.env.DB_CONNECTION | ||
}, | ||
test: { | ||
username: process.env.DB_USERNAME, | ||
password: process.env.DB_PASSWORD, | ||
database: process.env.DB_DATABASE, | ||
host: process.env.DB_HOST, | ||
dialect: process.env.DB_CONNECTION | ||
}, | ||
production: { | ||
username: process.env.DB_USERNAME, | ||
password: process.env.DB_PASSWORD, | ||
database: process.env.DB_DATABASE, | ||
host: process.env.DB_HOST, | ||
dialect: process.env.DB_CONNECTION | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const Sequelize = require('sequelize'); | ||
const process = require('process'); | ||
const basename = path.basename(__filename); | ||
const env = process.env.NODE_ENV || 'development'; | ||
const config = require(__dirname + '/../config/config.json')[env]; | ||
const db = {}; | ||
|
||
let sequelize; | ||
if (config.use_env_variable) { | ||
sequelize = new Sequelize(process.env[config.use_env_variable], config); | ||
} else { | ||
sequelize = new Sequelize(config.database, config.username, config.password, config); | ||
} | ||
|
||
fs | ||
.readdirSync(__dirname) | ||
.filter(file => { | ||
return ( | ||
file.indexOf('.') !== 0 && | ||
file !== basename && | ||
file.slice(-3) === '.js' && | ||
file.indexOf('.test.js') === -1 | ||
); | ||
}) | ||
.forEach(file => { | ||
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes); | ||
db[model.name] = model; | ||
}); | ||
|
||
Object.keys(db).forEach(modelName => { | ||
if (db[modelName].associate) { | ||
db[modelName].associate(db); | ||
} | ||
}); | ||
|
||
db.sequelize = sequelize; | ||
db.Sequelize = Sequelize; | ||
|
||
module.exports = db; |
Oops, something went wrong.