Skip to content

Commit

Permalink
simple node express server -crud
Browse files Browse the repository at this point in the history
rest api
  • Loading branch information
i-python-com committed Sep 11, 2019
0 parents commit 5114cb7
Show file tree
Hide file tree
Showing 10 changed files with 3,069 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Dependency directories
node_modules

# production directories
build

# dotenv environment variables file
.env
.env.test

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Simple Node Express Server

rest api endpoints

- get
- post
- patch
- delete
32 changes: 32 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const express = require('express')
const mongoose = require('mongoose')
const config = require('config')
const cors = require('cors')

const indexRouter = require('./routes/index')
const postsRouter = require('./routes/posts')

const app = express()

app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: false }))

// routes
app.use('/', indexRouter)
app.use('/posts', postsRouter)

const db = config.get('mongoURI')

// connect to DB
mongoose
.connect(db, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
})
.then(() => console.log('MongoDB connected...'))
.catch(err => console.log(err))

// listen to port
app.listen(3000, () => console.log('Server running on port 3000'))
4 changes: 4 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"mongoURI": "mongodb://localhost/test",
"jwtSecret": "sl_myJwtSecret"
}
18 changes: 18 additions & 0 deletions models/Post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const mongoose = require('mongoose')

const PostSchema = mongoose.Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
})

module.exports = mongoose.model('posts', PostSchema)
Loading

0 comments on commit 5114cb7

Please sign in to comment.