Created
March 14, 2019 14:48
-
-
Save 99darwin/c88890ca711375ad6b65b4bd63514311 to your computer and use it in GitHub Desktop.
Express handlebars server with MongoDB
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
const express = require('express') | |
const mongoose = require('mongoose') | |
const exphbs = require('express-handlebars') | |
const bodyParser= require('body-parser') | |
const path = require('path') | |
const PORT = process.env.PORT || 1279 | |
const app = express() | |
const routes = require('../routes') | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(bodyParser.text()); | |
app.use(bodyParser.json({ type: "application/vnd.api+json" })); | |
app.use(express.static(path.join(__dirname + '/public'))); | |
app.use(express.static(path.join(__dirname, "js"))); | |
app.engine('.hbs', exphbs({ | |
defaultLayout: 'main', | |
extname: '.hbs', | |
partialsDir: ['views/partials'] | |
})) | |
app.set('views' + '' + path.join(__dirname + '/views')) | |
app.set('view engine', '.hbs') | |
app.use(bodyParser.urlencoded({ extended: false })) | |
app.use(bodyParser.json()) | |
app.use(routes) | |
const DB_URL = 'mongodb://localhost:27017/your_db' | |
mongoose.Promise = Promise | |
if (mongoose.connection.readyState == 0) { | |
mongoose.connect(DB_URL, { useNewUrlParser: true }) | |
} | |
app.listen(PORT, () => { | |
console.log(`App listening on port: ${PORT}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment