├── .gitignore
├── images
├── Untitled.png
└── Untitled1.png
├── Config
└── mongoConfig.js
├── README.md
├── Models
├── blogSchema.js
└── userSchema.js
├── package.json
├── Middleware
└── authmiddleware.js
├── server.js
└── routes
├── authrouter.js
├── userRouter.js
└── blogrouter.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | node_modules/
3 | .env
--------------------------------------------------------------------------------
/images/Untitled.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shannathomp/Blog-Api/HEAD/images/Untitled.png
--------------------------------------------------------------------------------
/images/Untitled1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shannathomp/Blog-Api/HEAD/images/Untitled1.png
--------------------------------------------------------------------------------
/Config/mongoConfig.js:
--------------------------------------------------------------------------------
1 | const mongoose = require('mongoose')
2 | module.exports = async () => {
3 | try {
4 | await mongoose.connect(process.env.MONGODB_URI)
5 | mongoose.connection
6 | console.log('MongoDB connected');
7 | } catch (error) {
8 |
9 | }
10 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Installation:
2 | No Installation needed. Web app.
3 | ## Blogapi:
4 | This is a finance blog Api is just a backend database created for storage of users. Api just the middleman connecting and communicating the backend with the front end.
5 |
6 | ## Dependencies:
7 | bcrypt, dotenv, express, jsonwebtoken, mongoose, morgan, helmet.
8 |
9 | ## Main Features
10 |
11 |
12 |
13 |
14 | ## Demo
15 | 
16 |
--------------------------------------------------------------------------------
/Models/blogSchema.js:
--------------------------------------------------------------------------------
1 | const mongoose = require('mongoose')
2 |
3 |
4 | const blogSchema = mongoose.Schema({
5 | created_by: {
6 | type: String,
7 | required: true
8 | },
9 |
10 | blog_title: {
11 | type: String,
12 | required: true
13 | },
14 |
15 | blog_content:{
16 | type: String,
17 | required:true
18 | },
19 |
20 | private: {
21 | type: Boolean,
22 | default: false
23 | },
24 |
25 | created_at: {
26 | type: Date,
27 | default: Date.now()
28 | }
29 | })
30 |
31 | module.exports = mongoose.model('blog', blogSchema)
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "blog-api",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "server.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1",
8 | "start": "node server.js"
9 | },
10 | "keywords": [],
11 | "author": "Shanna Thompson",
12 | "license": "ISC",
13 | "dependencies": {
14 | "bcrypt": "^5.0.1",
15 | "dotenv": "^16.0.1",
16 | "express": "^4.18.1",
17 | "express-validator": "^6.14.1",
18 | "helmet": "^5.1.0",
19 | "jsonwebtoken": "^8.5.1",
20 | "mongoose": "^6.3.8",
21 | "morgan": "^1.10.0"
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/Middleware/authmiddleware.js:
--------------------------------------------------------------------------------
1 | const jwt = require('jsonwebtoken')
2 |
3 | module.exports = (req, res, next) => {
4 | // get the token from the headers object
5 | const token = req.header('x-auth-token')
6 |
7 | // If NO token
8 | if (!token) {
9 | return res.json('No Token Access denied!')
10 | }
11 | // If we have a token
12 | try {
13 | const decoded = jwt.verify(token, process.env.SECRET_KEY)
14 |
15 | console.log(decoded);
16 |
17 | next()
18 | } catch (error) {
19 | console.log(error);
20 | res.status(400).json('Token not valid')
21 | }
22 | }
--------------------------------------------------------------------------------
/Models/userSchema.js:
--------------------------------------------------------------------------------
1 |
2 | const mongoose = require('mongoose')
3 | const userSchema = mongoose.Schema({
4 | username: {
5 | type: String,
6 | required: true
7 | },
8 |
9 | email: {
10 | type: String,
11 | required: true,
12 | unique: true
13 | },
14 |
15 | birthday: {
16 | type: Date,
17 | required: true
18 | },
19 |
20 | age: {
21 | type: Number,
22 | required: true
23 | },
24 |
25 | password: {
26 | type: String,
27 | required: true,
28 | },
29 |
30 | Created_at: {
31 | type: Date,
32 | default: Date.now()
33 | }
34 | })
35 |
36 | module.exports = mongoose.model('user', userSchema)
37 |
--------------------------------------------------------------------------------
/server.js:
--------------------------------------------------------------------------------
1 | // PART ONE - Create your Server & Routers:
2 | // - Server
3 | // - Mongo Connected
4 | // - Middleware
5 | // - Routes Connected
6 | // - Routes
7 | // - /auth
8 | // - /blogs
9 |
10 | const express = require('express')
11 | const authrouter = require('./routes/authrouter')
12 | const blogrouter = require('./routes/blogrouter')
13 | const mongoConfig = require('./Config/mongoConfig')
14 | const userRouter = require('./routes/userRouter')
15 | const morgan = require('morgan')
16 | const helmet = require('helmet')
17 | require('dotenv').config()
18 | const app = express()
19 |
20 |
21 | app.use(express.json())
22 | app.use(morgan('dev'))
23 | app.use(helmet())
24 | const port = process.env.PORT || 3000
25 |
26 | app.use('/blogpost', blogrouter)
27 | app.use('/user', userRouter )
28 | app.use('/auth', authrouter)
29 |
30 | app.get('/',(req,res) => {
31 | // res.status(200).json()
32 | res.send('