Last active
January 15, 2018 13:18
-
-
Save barongun/d55233fd43ddcff371877da8261da805 to your computer and use it in GitHub Desktop.
express mongodb
This file contains hidden or 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
import {Router} from 'express' | |
import Test from '../model/test' | |
const router = Router() | |
router.get('/test1', function (req, res, next) { | |
Test.find(function (err, tests) { | |
if (err) return res.status(500).send({error: 'error'}) | |
res.json(tests) | |
}) | |
}) | |
export default router | |
This file contains hidden or 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
import express from 'express' | |
import { Nuxt, Builder } from 'nuxt' | |
import bodyParser from 'body-parser' | |
import mongoose from 'mongoose' | |
import api from './api' | |
import test from './test' | |
const app = express() | |
const host = process.env.HOST || '127.0.0.1' | |
const port = process.env.PORT || 3000 | |
app.set('port', port) | |
// Import API Routes | |
app.use('/api', api) | |
app.use('/test', test) | |
app.use(bodyParser.urlencoded({extended: true})) | |
app.use(bodyParser.json()) | |
// Import and Set Nuxt.js options | |
let config = require('../nuxt.config.js') | |
config.dev = !(process.env.NODE_ENV === 'production') | |
// Init Nuxt.js | |
const nuxt = new Nuxt(config) | |
// Build only in dev mode | |
if (config.dev) { | |
const builder = new Builder(nuxt) | |
builder.build() | |
} | |
let db = mongoose.connection | |
db.on('error', console.error) | |
db.once('open', function () { | |
console.log('Connection mongod server') | |
}) | |
mongoose.connect('mongodb://localhost/meistertask', { useMongoClient: true }) | |
mongoose.Promise = global.Promise; | |
// Give nuxt middleware to express | |
app.use(nuxt.render) | |
// Listen the server | |
app.listen(port, host) | |
console.log('Server listening on ' + host + ':' + port) // eslint-disable-line no-console |
This file contains hidden or 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
import mongoose from 'mongoose' | |
let Schema = mongoose.Schema | |
let testSchema = new Schema({ | |
name: String | |
}) | |
export default mongoose.model('test', testSchema) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment