Skip to content

Instantly share code, notes, and snippets.

@inkz
Created July 11, 2022 04:06
Show Gist options
  • Save inkz/7f7f595c3fd6330dc3d9edb507c25da3 to your computer and use it in GitHub Desktop.
Save inkz/7f7f595c3fd6330dc3d9edb507c25da3 to your computer and use it in GitHub Desktop.
import express from 'express'
import mongoose from 'mongoose'
const app = express()
const port = 3000
// const { BlogPost } = require('./models')
import {BlogPost} from './models'
async function main() {
// test1 123
const conn = await mongoose.connect('mongodb://localhost:27017/my_database')
console.log('lets go')
app.use(express.json())
app.get('/', (req, res) => {
res.send(`Hello World! ${req.body && req.body.foobar}`)
})
app.post('/test', async (req, res) => {
const result = await BlogPost.find({title: req.body.title})
res.send(result)
})
app.post('/generate', async (req, res) => {
const bp = new BlogPost({title: 'Zen'});
await bp.save();
res.send('ok!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
}
;(async () => {
main()
})()
import mongoose from 'mongoose'
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;
const BlogPostSchema = new Schema({
author: ObjectId,
title: String,
body: String,
date: Date
})
const BlogPost = mongoose.model('BlogPost', BlogPostSchema)
export {BlogPost}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment