You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.2 KiB
JavaScript

2 months ago
// Declare dependencies.
const express = require('express')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
require('dotenv').config()
// Declare constants.
const app = express()
const defaultPort = 4000
const designatedPort = process.env.PORT || defaultPort
// Declare middlewares.
app.use(bodyParser.json())
// Enable the Cross Origin Resource Sharing (CORS).
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization')
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS')
next()
})
// Declare database connection.
mongoose.connect(process.env.MONGODB_SRV, {
useNewUrlParser:true,
useCreateIndex: true,
useUnifiedTopology: true
})
mongoose.connection.once('open', () => {
console.log('Connection to MongoDB Atlas has been successfully tested.')
}).catch(function(err) {
console.log(err)
})
// Declare routes.
app.use('/api', require('./routes'))
// Listen to server requests.
app.listen(designatedPort, () => {
console.log('Node.js backend now online in port ' + designatedPort)
})