Skip to content

Instantly share code, notes, and snippets.

@99darwin
Created March 14, 2019 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 99darwin/c88890ca711375ad6b65b4bd63514311 to your computer and use it in GitHub Desktop.
Save 99darwin/c88890ca711375ad6b65b4bd63514311 to your computer and use it in GitHub Desktop.
Express handlebars server with MongoDB
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