Skip to content

Commit 3032b79

Browse files
authored
Merge pull request #295 from Paarth11/main
add blog-application
2 parents 23fc0e3 + efe76fe commit 3032b79

33 files changed

+655
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
.env
3+
package-lock.json
4+
5+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "blog",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"start": "node src/app.js",
8+
"devStart": "nodemon src/app.js"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"cookie-parser": "^1.4.6",
15+
"dotenv": "^16.3.1",
16+
"ejs": "^3.1.9",
17+
"express": "^4.18.2",
18+
"jsonwebtoken": "^9.0.0",
19+
"mongoose": "^7.3.1",
20+
"multer": "^1.4.5-lts.1"
21+
},
22+
"devDependencies": {
23+
"nodemon": "^2.0.22"
24+
}
25+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require('dotenv').config();
2+
const express = require('express');
3+
const path = require('path');
4+
const app = express();
5+
const PORT = 3000;
6+
const mongoose = require('mongoose');
7+
const userRouter = require('./routes/user.js');
8+
const blogRouter = require('./routes/blog.js');
9+
const cookieParser = require('cookie-parser');
10+
const checkForAuthenticationCookie = require('./middleware/authentication.js');
11+
const Blog = require('./models/blog.js');
12+
13+
app.listen(PORT, () => {
14+
console.log(`Server is running on port ${PORT}`);
15+
});
16+
17+
mongoose.connect('mongodb://127.0.0.1:27017/Blogify').then(() => {
18+
console.log('connected to db');
19+
});
20+
21+
app.set('view engine', 'ejs');
22+
app.set('views', path.join(__dirname, 'views'));
23+
24+
app.use(express.urlencoded({ extended: false })); // for parsing application/x-www-form-urlencoded
25+
app.use(cookieParser());
26+
app.use(checkForAuthenticationCookie('token'));
27+
app.use(express.static(path.join(__dirname, './public')));
28+
29+
app.use('/user', userRouter);
30+
app.use('/blog', blogRouter);
31+
32+
app.get('/', async (req, res) => {
33+
const allBlogs = await Blog.find({});
34+
res.render('home', {
35+
user: req.user,
36+
blogs: allBlogs,
37+
38+
});
39+
});
40+
41+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const User = require('../../models/user.js');
2+
3+
const login = async (req, res) => {
4+
const { email, password } = req.body;
5+
try {
6+
const token = await User.matchPasswordAndGenerateToken(email, password);
7+
return res.cookie('token', token).redirect('/');
8+
} catch (err) {
9+
res.render('signIn', { error: err.message });
10+
}
11+
};
12+
13+
module.exports = login;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
const logout = (req,res)=>{
3+
res.clearCookie('token').redirect('/')
4+
}
5+
6+
module.exports = logout;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const User = require('../../models/user')
2+
3+
const register = async(req,res)=>{
4+
const {fullName,email,password} = req.body;
5+
await User.create({
6+
fullName,
7+
email,
8+
password
9+
});
10+
return res.redirect('/user/signin')
11+
}
12+
13+
module.exports = register;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const signIn = (req,res)=>{
2+
return res.render('signIn')
3+
}
4+
5+
module.exports = signIn;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
const signUp = (req,res)=>{
3+
return res.render('signUp')
4+
}
5+
6+
module.exports = signUp;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const multer = require('multer');
2+
const path = require('path');
3+
4+
const storage = multer.diskStorage({
5+
destination: function (req, file, cb) {
6+
cb(null, path.resolve(`./src/public/uploads/`));
7+
},
8+
filename: function (req, file, cb) {
9+
const fileName = `${Date.now()}-${file.originalname}`;
10+
cb(null, fileName);
11+
},
12+
});
13+
14+
module.exports = storage;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
const addBlog = (req,res)=>{
3+
return res.render('addBlog',{
4+
user: req.user
5+
})
6+
}
7+
8+
module.exports = addBlog;

0 commit comments

Comments
 (0)