blog image grid-fs
This commit is contained in:
parent
dad033cc4b
commit
df0d19b106
27
app.js
27
app.js
|
@ -1,4 +1,5 @@
|
|||
const express = require("express");
|
||||
const app = express();
|
||||
const mongoose = require("mongoose");
|
||||
const bodyparser = require("body-parser");
|
||||
const authRoute = require("./routes/auth");
|
||||
|
@ -11,7 +12,6 @@ const adminRoute = require("./routes/admin");
|
|||
const blogRouter = require("./routes/blog");
|
||||
const port = process.env.PORT || 5000;
|
||||
const cors = require("cors");
|
||||
const app = express();
|
||||
|
||||
//const passport = require('passport');
|
||||
//const cookieSession = require('cookie-session') ;
|
||||
|
@ -75,21 +75,18 @@ app.use(bodyparser.json());
|
|||
// req.logout() ;
|
||||
// res.redirect('/') ;
|
||||
// })
|
||||
mongoose
|
||||
.connect(MONGO_URI, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
useFindAndModify: false,
|
||||
})
|
||||
.then((result) => {
|
||||
console.log("connected");
|
||||
app.listen(port, () => {
|
||||
console.log("server is running on port", port);
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
|
||||
mongoose.connect(MONGO_URI, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
useFindAndModify: false,
|
||||
});
|
||||
let conn = mongoose.connection.on("connected", (res) => {
|
||||
console.log("Connected to MongoDB");
|
||||
app.listen(port, () => {
|
||||
console.log("Server Listening on Port", port);
|
||||
});
|
||||
});
|
||||
app.use(authRoute);
|
||||
|
||||
app.use(profileRoute);
|
||||
|
|
|
@ -8,6 +8,10 @@ const messagebird = require("messagebird")("llVKD53ve6QRpbCKOHzWBADaS", null, [
|
|||
]);
|
||||
const nodemailer = require("nodemailer");
|
||||
const smtpTransport = require("nodemailer-smtp-transport");
|
||||
// const { OAuth2Client } = require("google-auth-library");
|
||||
// const client = new OAuth2Client(
|
||||
// "7810129519-dr5l4l1i7a7bh07sbvl49gd80coenphj.apps.googleusercontent.com"
|
||||
// );
|
||||
|
||||
// -------------------------------------------- mail transporter -----------------------------------------
|
||||
|
||||
|
@ -148,6 +152,24 @@ module.exports.postSignin = async (req, res, next) => {
|
|||
console.log(err);
|
||||
}
|
||||
};
|
||||
// Gmail Login Starts.
|
||||
// -----------------------------------------------------------------------------------------------
|
||||
|
||||
//1026548376782-5p5tjck8ffhan9l1ajhv6orr87dfkrrf.apps.googleusercontent.com
|
||||
|
||||
// module.exports.googleSignIn = async (req, res, next) => {
|
||||
// const { tokenId } = req.params;
|
||||
// console.log("TokenId from frontend", tokenId);
|
||||
// client
|
||||
// .verifyIdToken({
|
||||
// idToken: tokenId,
|
||||
// audience:
|
||||
// "7810129519-dr5l4l1i7a7bh07sbvl49gd80coenphj.apps.googleusercontent.com",
|
||||
// })
|
||||
// .then((response) => {
|
||||
// console.log(response.payload);
|
||||
// });
|
||||
// };
|
||||
|
||||
// Phone verification Starts.
|
||||
// -----------------------------------------------------------------------------------------------
|
||||
|
|
|
@ -1,12 +1,37 @@
|
|||
const { log } = require("handlebars");
|
||||
const Blog = require("../models/Blog");
|
||||
const User = require("../models/User");
|
||||
const mongoose = require("mongoose");
|
||||
const GridFsStorage = require("multer-gridfs-storage");
|
||||
const Grid = require("gridfs-stream");
|
||||
const multer = require("multer");
|
||||
const MONGO_URI = `mongodb+srv://Cantilever:Cantilever@cluster0.dqxva.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
|
||||
|
||||
const connect = mongoose.createConnection(MONGO_URI, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
});
|
||||
let gfs;
|
||||
connect.once("open", () => {
|
||||
// initialize stream
|
||||
gfs = Grid(connect.db, mongoose.mongo);
|
||||
gfs.collection("blogImages");
|
||||
});
|
||||
|
||||
module.exports.getAllBlogs = async (req, res, next) => {
|
||||
try {
|
||||
let blog = await Blog.find();
|
||||
if (blog) {
|
||||
res.json({ blogs: blog });
|
||||
gfs.files.find().toArray((err, files) => {
|
||||
//check if files exist
|
||||
if (!files || files.length == 0) {
|
||||
return res.status(404).json({
|
||||
err: "No files exist",
|
||||
});
|
||||
}
|
||||
// files exist
|
||||
res.json({ files, blogs: blog });
|
||||
});
|
||||
} else {
|
||||
console.log("Error in Blog", blog);
|
||||
return;
|
||||
|
@ -18,9 +43,26 @@ module.exports.getAllBlogs = async (req, res, next) => {
|
|||
}
|
||||
};
|
||||
|
||||
module.exports.addBlog = async (req, res, next) => {
|
||||
const { _id, title, body, image, author } = req.body; //_id is of user from frontend who is adding the blog.
|
||||
module.exports.getSingleBlog = async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
let blog = await Blog.findById({ _id: id });
|
||||
if (blog) {
|
||||
res.json({ blog });
|
||||
} else {
|
||||
res.json({ error: "Blog Not Found" });
|
||||
}
|
||||
} catch {
|
||||
(err) => {
|
||||
console.log("Error", err);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.addBlog = async (req, res, next) => {
|
||||
const { _id, title, body, author } = req.body; //_id is of user from frontend who is adding the blog.
|
||||
try {
|
||||
console.log("Image File", req.file);
|
||||
let user = await User.findById({ _id });
|
||||
if (!title) {
|
||||
res.status(422).json({ message: "Please, Add Title of the Blog" });
|
||||
|
@ -34,8 +76,8 @@ module.exports.addBlog = async (req, res, next) => {
|
|||
let blog = new Blog({
|
||||
title,
|
||||
body,
|
||||
image,
|
||||
date: new Date(),
|
||||
image: req.file,
|
||||
date: new Date().toISOString(),
|
||||
author,
|
||||
});
|
||||
await blog.save();
|
||||
|
|
|
@ -10,7 +10,7 @@ const blogSchema = new mongoose.Schema({
|
|||
type: Date,
|
||||
},
|
||||
image: {
|
||||
type: String,
|
||||
type: Object,
|
||||
default: "",
|
||||
},
|
||||
body: {
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -19,11 +19,15 @@
|
|||
"crypto": "^1.0.1",
|
||||
"dotenv": "^9.0.1",
|
||||
"express": "^4.17.1",
|
||||
"google-auth-library": "^7.0.4",
|
||||
"grandjs": "^2.2.30",
|
||||
"gridfs-stream": "^1.1.1",
|
||||
"handlebars": "^4.7.7",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"messagebird": "^3.6.1",
|
||||
"mongoose": "^5.12.2",
|
||||
"multer": "^1.4.2",
|
||||
"multer-gridfs-storage": "^4.2.0",
|
||||
"nodemailer": "^6.6.0",
|
||||
"nodemailer-smtp-transport": "^2.7.4",
|
||||
"passport": "^0.4.1",
|
||||
|
|
312
readme.md
312
readme.md
|
@ -1 +1,311 @@
|
|||
Cantileverlabs Nodejs Backend API Documentation.
|
||||
# Cantileverlabs Nodejs Backend API Documentation.
|
||||
|
||||
## Project Structure :
|
||||
|
||||
```
|
||||
/root
|
||||
|- controllers
|
||||
| |-- admin.js
|
||||
| |-- auth.js
|
||||
| |-- blog.js
|
||||
| |-- coupon.js
|
||||
| |-- course.js
|
||||
| |-- payment.js
|
||||
| |-- profile.js
|
||||
| |-- query.js
|
||||
|
|
||||
|- middlewares
|
||||
| |-- isAdmin.js
|
||||
| |-- requireLogin.js
|
||||
|
|
||||
|- models
|
||||
| |-- Blog.js
|
||||
| |-- Coupon.js
|
||||
| |-- Course.js
|
||||
| |-- CourseType.js
|
||||
| |-- Order.js
|
||||
| |-- query.js
|
||||
| |-- Student.js
|
||||
| |-- User.js
|
||||
|
|
||||
|- routes
|
||||
| |-- admin.js
|
||||
| |-- auth.js
|
||||
| |-- blog.js
|
||||
| |-- Coupon.js
|
||||
| |-- course.js
|
||||
| |-- payment.js
|
||||
| |-- profile.js
|
||||
| |-- query.js
|
||||
|
|
||||
|- app.js
|
||||
```
|
||||
|
||||
### App will listen on the port `http://localhost:5000`
|
||||
|
||||
# Routes Functions and Body Parameters.
|
||||
|
||||
## File :- Admin.js
|
||||
|
||||
```
|
||||
Route : /addSchedule
|
||||
Method: POST
|
||||
Requirements : |-- isAuth
|
||||
|-- isAdmin
|
||||
|
||||
Route : /editSchedule
|
||||
Method: POST
|
||||
Requirements : |-- isAuth
|
||||
|-- isAdmin
|
||||
|
||||
Route : /deleteSchedule
|
||||
Method: POST
|
||||
Requirements : |-- isAuth
|
||||
|-- isAdmin
|
||||
|
||||
```
|
||||
|
||||
## File :- Auth.js
|
||||
|
||||
### Note: `All the Body Params are as it is`
|
||||
|
||||
```
|
||||
Route : /protected
|
||||
Method: GET
|
||||
Requirements : |-- isAuth
|
||||
|
||||
|
||||
Route : /signup
|
||||
Method: POST
|
||||
Body Params :- firstName
|
||||
- lastName
|
||||
- email *must*
|
||||
- password *must*
|
||||
---------- for sending the Verification Emial to user.
|
||||
- sending_company_email
|
||||
- subject
|
||||
Response : {
|
||||
message:"You are Registered, Please Login"
|
||||
}
|
||||
Response if email verification: {
|
||||
message:"Email sent with 6 Digit OTP , Check your Email"
|
||||
}
|
||||
|
||||
Route : /verifyemail
|
||||
Method: POST
|
||||
Body Params: - otp
|
||||
|
||||
Route : /signin
|
||||
Method: POST
|
||||
Body Params : - email
|
||||
- password
|
||||
|
||||
Route : /sendotp
|
||||
Method: POST
|
||||
Body Params : phoneNumber
|
||||
Respone : {id:id} id of the transaction.
|
||||
|
||||
Route : /getotp
|
||||
Method: POST
|
||||
Body Params : id , otp
|
||||
Response : {message:"Code Verified"}
|
||||
|
||||
Route : /forgotpassword
|
||||
Method: POST
|
||||
Body Params : email,link,sending_company_email,subject.
|
||||
|
||||
{ - email of the user. }
|
||||
{ - link is where to send the user for resetting a password. }
|
||||
{ - sending_company_email is email address of the comapny to send the email to the user. }
|
||||
{ - subject is email subject }
|
||||
|
||||
Route : /resetpassword/:_id/:token
|
||||
Method: POST
|
||||
Headers Params : _id, token
|
||||
Body Params : password
|
||||
Response : {message:"Password Updated!"}
|
||||
|
||||
```
|
||||
|
||||
## File :- BLog.js
|
||||
|
||||
```
|
||||
Route : /addBlog
|
||||
Method: POST
|
||||
Body Params : _id , title , body , author , image
|
||||
|
||||
String : { _id : Database _id of a person who is adding up a blog}
|
||||
String : { title : title of the blog}
|
||||
String: { body : body of the blog}
|
||||
String: image of the blog to be added}
|
||||
String : Author of the blog}
|
||||
|
||||
|
||||
Route : /getAllBlogs
|
||||
Method: GET
|
||||
Body Params : none
|
||||
|
||||
Route : /deleteBlog
|
||||
Method: POST
|
||||
Body Params : _id * must *
|
||||
|
||||
{ _id : _id is a id of the blog to delete.}
|
||||
|
||||
Route : /editBlog
|
||||
Method: POST
|
||||
Body Params : _id * must * , body , title , image.
|
||||
|
||||
String : { _id : Database _id of a person who is adding up a blog}
|
||||
String : { title : title of the blog}
|
||||
String: { body : body of the blog}
|
||||
String: image of the blog to be added}
|
||||
|
||||
```
|
||||
|
||||
## File :- Coupon.js
|
||||
|
||||
```
|
||||
Route : /set-coupon
|
||||
Method: POST
|
||||
Body Params : percentage , coupon_code , remainingTimes
|
||||
|
||||
String : { couponCode : Coupon code of the coupon}
|
||||
String: { percentage: Discount percentage of the coupon}
|
||||
String: { numAllowed : No of times the coupon is allwoed}
|
||||
|
||||
Route : /getAllCoupons
|
||||
Method: GET
|
||||
Body Params : none
|
||||
|
||||
Route : /deleteCoupon
|
||||
Method: POST
|
||||
Requirements : isAdmin, isAuth
|
||||
Body Params : couponCode * must *
|
||||
|
||||
{ couponCode : coupon code of the coupon }
|
||||
|
||||
Route : /addCoupon
|
||||
Method: POST
|
||||
Requirements : isAdmin, isAuth
|
||||
Body Params : couponCode * must * , percentage , numAllowed
|
||||
|
||||
String : { couponCode : Coupon code of the coupon}
|
||||
String: { percentage: Discount percentage of the coupon}
|
||||
String: { numAllowed : No of times the coupon is allwoed}
|
||||
|
||||
```
|
||||
|
||||
## File Course.js
|
||||
|
||||
```
|
||||
Route : /addCourse
|
||||
Method : POST
|
||||
Requirements : isAdmin , isAuth
|
||||
Body Params : name , mentor , totalLectures , route
|
||||
|
||||
Route : /getAllCourse
|
||||
Method : GET
|
||||
Requirements : None
|
||||
Body Params : None
|
||||
|
||||
|
||||
Route : /getSingleParCourse
|
||||
Method : POST
|
||||
Body Params : parCourseId
|
||||
|
||||
|
||||
Route : /getMeetSchedule
|
||||
Method : POST
|
||||
Requiremnents : isAuth
|
||||
Body Params : courseId
|
||||
|
||||
|
||||
Route : /getFromRoute
|
||||
Method : GET
|
||||
Requirements : None
|
||||
Query Params : route
|
||||
|
||||
|
||||
Route : /addClick
|
||||
Method : POST
|
||||
Requiremnts : isAuth
|
||||
Body Params : route , userId
|
||||
|
||||
```
|
||||
|
||||
## File Payments.js
|
||||
|
||||
```
|
||||
|
||||
Route : /verification
|
||||
Method : POST
|
||||
Requirements : None
|
||||
Body Params :
|
||||
|
||||
|
||||
Route : /razorpay
|
||||
Method : POST
|
||||
Requirements : requireLogin
|
||||
Body Params : courseId , couponCode
|
||||
|
||||
```
|
||||
|
||||
## File Profle.js
|
||||
|
||||
```
|
||||
Route : getProfile
|
||||
Method : GET
|
||||
Requirements : requireLogin
|
||||
Body Params : _id of the User.
|
||||
|
||||
|
||||
Route : postProfile
|
||||
Method : POST
|
||||
Requirements : requireLogin
|
||||
Body Params : _id , interests , projects , yearofgrad , phoneNumber , institute , skills
|
||||
|
||||
Route : deleteUser
|
||||
Method : POST
|
||||
Requirements : requireLogin
|
||||
Body Params : _id
|
||||
{_id : _id of the User to delete the Profile}
|
||||
|
||||
|
||||
Route : editUser
|
||||
Method : POST
|
||||
Requirements : requireLogin
|
||||
Body Params : _id , newFirstName , newLastName ,email
|
||||
|
||||
{_id : _id of the user to edit the profile}
|
||||
{newFirstName : New Name }
|
||||
{newLastName : New last name}
|
||||
{email : if email has to be changed}
|
||||
|
||||
```
|
||||
|
||||
## File Query.js
|
||||
|
||||
```
|
||||
Route : addQuery
|
||||
Method : POST
|
||||
Requirements : None
|
||||
Body Params : fullName , phoneNumber , email , query , other , graduationYear
|
||||
|
||||
|
||||
Route : getAllQueries
|
||||
Method : GET
|
||||
Requirements : None
|
||||
Body Params : None
|
||||
|
||||
|
||||
Route : getOtherQueries
|
||||
Method : GET
|
||||
Requirements : None
|
||||
Body Params : None
|
||||
|
||||
|
||||
Route : getTypeQueries
|
||||
Method : POST
|
||||
Requirements : None
|
||||
Body Params : type
|
||||
```
|
||||
|
|
|
@ -17,6 +17,8 @@ router.post("/forgotpassword", authController.forgotpassword);
|
|||
|
||||
router.post("/resetpassword/:_id/:token", authController.resetpassword);
|
||||
|
||||
// router.post("/googleSignIn/:tokenId", authController.googleSignIn);
|
||||
|
||||
// router.post("/verifyemail", authController.verfiyemail);
|
||||
|
||||
module.exports = router;
|
||||
|
|
|
@ -2,11 +2,31 @@ const express = require("express");
|
|||
const router = express.Router();
|
||||
const BlogController = require("../controllers/blog");
|
||||
const isAuth = require("../middleware/requirelogin");
|
||||
const GridFsStorage = require("multer-gridfs-storage");
|
||||
const Grid = require("gridfs-stream");
|
||||
const multer = require("multer");
|
||||
const MONGO_URI = `mongodb+srv://Cantilever:Cantilever@cluster0.dqxva.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
|
||||
|
||||
router.post("/addBlog", BlogController.addBlog);
|
||||
let storage = new GridFsStorage({
|
||||
url: MONGO_URI,
|
||||
file: (req, file) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const fileInfo = {
|
||||
filename: file.originalname,
|
||||
bucketName: "blogImages",
|
||||
};
|
||||
resolve(fileInfo);
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const upload = multer({ storage });
|
||||
router.post("/addBlog", upload.single("file"), BlogController.addBlog);
|
||||
|
||||
router.get("/getAllBlogs", BlogController.getAllBlogs);
|
||||
|
||||
router.get("/blog/:id", BlogController.getSingleBlog);
|
||||
|
||||
router.post("/deleteBlog/", BlogController.deleteBlog);
|
||||
|
||||
router.post("/editBlog/", BlogController.editBlog);
|
||||
|
|
Loading…
Reference in New Issue