Hey am making a website that will show the articles and the relevant images at the same time. I have already been uploading image files but I cannot show it in my page.
These are my relevant codes:
Article Module:
let mongoose = require('mongoose');
let articleSchema = mongoose.Schema({
title:{
type: String,
required: true
},
author:{
type:String,
required:true
},
body:{
type:String,
required:true
},
img:{
type:String,
required:true
}
});
let Article = module.exports = mongoose.model('Article', articleSchema);
User Module:
let mongoose = require('mongoose');
// User Schema
let UserSchema = mongoose.Schema({
name:{
type: String,
required: true
},
email:{
type: String,
required: true
},
username:{
type: String,
required: true
},
password:{
type: String,
required: true
}
});
let User = module.exports = mongoose.model('User', UserSchema);
Article.js:
const express = require('express');
const router = express.Router();
const multer = require('multer');
// Article Model
let Article = require('../models/article');
// User Model
let User = require('../models/user');
router.get('/sonuc', function(req, res){
if(req.query.search) {
const regex = new RegExp(escapeRegex(req.query.search), 'gi');
Article.find({"title": regex}, function(err, articles) {
if(err) {
console.log(err);
} else {
res.render("sonuc", {
articles: articles
});
}
});
}
});
function escapeRegex(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};
// Add Route
router.get('/add', ensureAuthenticated, function(req, res){
res.render('add_article', {
title:'Add Article'
});
});
const upload = multer({dest: __dirname + '/uploads'});
// Add Submit POST Route
router.post('/add',upload.single('img'), function(req, res){
req.checkBody('title','Title is required').notEmpty();
//req.checkBody('author','Author is required').notEmpty();
req.checkBody('body','Body is required').notEmpty();
// Get Errors
let errors = req.validationErrors();
if(errors){
res.render('add_article', {
title:'Add Article',
errors:errors
});
} else {
var article = new Article({
title: req.body.title,
author: req.user._id,
img: '/uploads/' + req.file.filename,
body: req.body.body,
});
article.save(function(err){
if(err){
console.log(err);
return;
} else {
res.render('add_article', {
});
}
});
}
});
// Load Edit Form
router.get('/edit/:id', ensureAuthenticated, function(req, res){
Article.findById(req.params.id, function(err, article){
/*if(article.author != req.user._id){
req.flash('danger', 'Not Authorized');
return res.redirect('/');
}*/
res.render('edit_article', {
title:'Edit Article',
article:article
});
});
});
// Update Submit POST Route
router.post('/edit/:id', function(req, res){
let article = {};
article.title = req.body.title;
article.author = req.body.author;
article.body = req.body.body;
let query = {_id:req.params.id}
Article.update(query, article, function(err){
if(err){
console.log(err);
return;
} else {
req.flash('success', 'Article Updated');
res.redirect('/');
}
});
});
// Delete Article
router.delete('/:id', function(req, res){
if(!req.user._id){
res.status(500).send();
}
let query = {_id:req.params.id}
Article.findById(req.params.id, function(err, article){
if(article.author != req.user._id){
res.status(500).send();
} else {
Article.remove(query, function(err){
if(err){
console.log(err);
}
res.send('Success');
});
}
});
});
// Get Single Article
router.get('/:id', function(req, res){
Article.findById(req.params.id, function(err, article){
//User.findById(article.author, function(err, user){
res.render('article', {
article:article,
imagePath: user.img,
author: user.name
});
});
});
//});
// Access Control
function ensureAuthenticated(req, res, next){
if(req.isAuthenticated()){
return next();
} else {
req.flash('danger', 'Please login');
res.redirect('/users/login');
}
}
module.exports = router;
article.pug:
extends layout
block content
br
h1= article.title
//h5 Written by #{author}
p= article.body
img(src=imagePath, alt='img')
hr
if user
if user.id ==article.author
a.btn.btn-default(href='/articles/edit/'+article._id) Edit
a.btn.btn-danger.delete-article(href='#',data-id=article._id) Delete
Still I cannot understan why that these codes are not working. I am waiting your supports. Every answer will be appreciated.
My App.js:
const express = require('express');
const ejs = require('ejs');
const path = require('path');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const expressValidator = require('express-validator');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
config = require('./config/database');
const multer = require('multer');
mongoose.connect(config.database);
let db = mongoose.connection;
// Check connection
db.once('open', function(){
console.log('Connected to MongoDB');
});
// Check for DB errors
db.on('error', function(err){
console.log(err);
});
// Init App
const app = express();
// Bring in Models
let Article = require('./models/article');
// EJS
app.set('view engine', 'ejs');
// Public Folder
app.use(express.static('./public'));
// Load View Engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// Body Parser Middleware
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
// Set Public Folder
app.use(express.static(path.join(__dirname, 'public')));
// Express Session Middleware
app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true
}));
// Express Messages Middleware
app.use(require('connect-flash')());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
// Express Validator Middleware
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam,
msg : msg,
value : value
};
}
}));
// Passport Config
require('./config/passport')(passport);
// Passport Middleware
app.use(passport.initialize());
app.use(passport.session());
app.get('*', function(req, res, next){
res.locals.user = req.user || null;
next();
});
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
const upload = multer({ storage: storage })
// Check File Type
function checkFileType(file, cb){
// Allowed ext
const filetypes = /jpeg|jpg|png|gif/;
// Check ext
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
// Check mime
const mimetype = filetypes.test(file.mimetype);
if(mimetype && extname){
return cb(null,true);
} else {
cb('Error: Images Only!');
}
}
// Home Route
app.get('/', function(req, res){
Article.find({}, function(err, articles){
if(err){
console.log(err);
} else {
res.render('index', {
title:'Articles',
articles: articles
});
}
});
});
app.get('/sonuc', function(req, res){
if (req.query.search) {
const regex = new RegExp(escapeRegex(req.query.search), 'gi');
Article.find({"title": regex}, function(err, articles) {
if(err) {
console.log(err);
} else {
res.render("index", {
articles: articles
});
}
});
}
});
function escapeRegex(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};
// Route Files
let articles = require('./routes/articles');
let users = require('./routes/users');
app.use('/articles', articles);
app.use('/users', users);
// Start Server
app.listen(3000, function(){
console.log('Server started on port 3000...');
});
You have to pass in the path to the img tag for it to render your image.
You have specified /upload to be the folder, so just put that into the src attribute of the img tags.
extends layout
block content
br
h1= article.title
//h5 Written by #{author}
p= article.body
img(src=imagePath, alt='img')
GET route fix:
Article.findById(req.params.id, function(err, article){
//You commented this line that's why user is undefined
User.findById(article.author, function(err, user){
res.render('article', {
article:article,
imagePath: article.img, //Should be article.img
author: user.name
})
Related
I am trying to learn new things with Express and at this moment I would like to be able to show express messages. I added flash, session, and express-messages as dependencies and added messages as a locals variable.
In Pug, I added != messages('message'. locals) and according to the tutorial this should've output a div element with the messages, but unfortunately it throws an error "messages is not a function" when I tried to run it. I suppose req.flash('info','Article Added') in the index.js file on the server side will trigger the message.
this is the screenshot of the error I keep getting
Pls check if I'm getting anything wrong in my code, I'm stuck here. I had tried so much to find out what had caused it.
Here here is my app.js file
const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const expressValidator = require('express-validator');
const session = require('express-session');
const flash = require('connect-flash');
mongoose.connect('mongodb://localhost:27017/nodekb', { useNewUrlParser: true });
const db = mongoose.connection;
const app = express();
// app.dynamicHelpers({ messages: require('express-messages') });
//checking for db's connection
db.once('open', () => {
console.log('connected to mongo db');
});
//checking for db errors
db.on('error', (err) => {
console.log(err);
});
//setting up view engine directories
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
//bring in model
let Article = require('./models/articles')
//express routing and using the article model for home page
app.get('/', (req, res) => {
Article.find({}, (err, articles) => {
if (err) {
console.log(err)
} else {
res.render('index', {
title: 'Article',
articles: articles
});
}
});
});
//body parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
//seet public folder
app.use(express.static(path.join(__dirname, 'Public')));
//express session middleware
app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true
}))
//express message middleware
app.use(flash());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
//
app.use(expressValidator({
errorFormatter: (param, msg, value) => {
var namespace = param.split('.'),
root = namespace.shift(),
formParam = root;
while (namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param: formParam,
msg: msg,
value: value
}
}
}))
//another route to add article
app.get('/article/add', (req, res) => {
res.render('add_article', {
title: 'Add Article'
});
});
//get single article
app.get('/article/:id', (req, res) => {
Article.findById(req.params.id, (err, article) => {
res.render('article', {
article: article
})
return;
});
});
//edit single article
app.get('/article/edit/:id', (req, res) => {
Article.findById(req.params.id, (err, article) => {
res.render('edit_article', {
title: 'Edit Article',
article: article
})
return;
});
});
//submit POST form from add_article.pug
app.post('/article/add', (req, res) => {
let article = new Article();
article.title = req.body.title;
article.author = req.body.author;
article.body = req.body.body;
article.save((err) => {
if (err) {
console.log(err);
} else {
req.flash('info', 'Article Added');
// res.send(article);
res.redirect('/');
}
})
});
//update POST form from edit_article.pug
app.post('/article/add/edit/:id', (req, res) => {
let article = {};
let query = { _id: req.params.id }
article.title = req.body.title;
article.author = req.body.author;
article.body = req.body.body;
Article.update(query, article, (err) => {
if (err) {
console.log(err);
} else {
req.flash('info', 'Article updated');
res.redirect('/');
}
})
});
app.delete('/article/:id', (req, res) => {
let query = { _id: req.params.id }
Article.remove(query, (err) => {
if (err) {
console.log(err);
};
res.send('sucess')
})
})
//setting up port
app.listen(3000, () => {
console.log('runnig on port 3000');
})
module.exports = app;
Here is my layout.pug where i am using the message
doctype html
html
head
title Knowledge base
link(rel='stylesheet', href='/bower_components/bootstrap/dist/css/bootstrap.css')
link(rel='stylesheet', href='/css/style.css')
body
nav.navbar.navbar-dark.bg-dark.navbar-expand-md
a.navbar-brand(href='#') Knowledge Base
button.navbar-toggler(type='button', data-toggle='collapse', data-target='#navbarsExample01', aria-controls='navbarsExample01', aria-expanded='false', aria-label='Toggle navigation')
span.navbar-toggler-icon
#navbarsExample01.collapse.navbar-collapse
ul.navbar-nav.ml-auto.pr-3
li.mr-2
a(href='/') Home
span.sr-only (current)
li.mr-2
a(href='/article/add') Add Articles
.container
!= messages('message', locals)
block content
br
hr
footer
p copyright © 2018
script(src='/bower_components/jquery/dist/jquery.js')
script(src='/bower_components/bootstrap/dist/js/bootstrap.js')
script(src='/js/main.js')
And finally the message.pug file referred to in Layout.pug
.messages
each type in Object.keys({messages})
each message in Object.values({messages})
div(class="alert alert-" +type) #{message}
You need to install the library express-messages
MEAN Stack
I keep getting
TypeError: Cannot read property 'first_name' of undefined
when trying to execute this code (all variables, including modules are defined in other parts of the code). While using GET Method, my code is working fine. But for POST Method, it throws error. Attached the screenshot below.
Advance Rest Client POST Method Exception
Added my code below.
Thanks in advance
//app.js
//Importing Modules
var express = require('express');
var mongoose = require('mongoose');
var bodyparser = require('body-parser');
var cors = require('cors');
var path = require('path');
var app = express();
//port Number
const port = 3000;
const route = require('./routes/route');
//Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/contactlist');
//if connected
mongoose.connection.on('connected', () => {
console.log("Mongo DB Connected successfully");
});
//if not connected
mongoose.connection.on('error', (err) => {
if (err) {
console.log('Error in connecting to the MongoDB');
}
});
//adding middleware cors
app.use(cors());
//routes will happen here
app.use('/api', route);
//adding middleware
app.use(bodyparser.json());
//Static files
app.use(express.static(path.join(__dirname, 'public')));
//Testing
app.get('/', (req, res) => {
res.send("Hello Vinoth");
});
app.get('/yahoo', (req, res) => {
res.send("Hello Vinoth");
});
//Connection
app.listen(port, () => {
console.log("Server started at port:" + port);
});
//route.js
const express = require('express');
const router = express.Router();
const Contact = require('../models/contacts');
// retrieving details
router.get('/contacts', (req, res, next) => {
Contact.find(function(err, contacts) {
res.json(contacts);
});
});
// Add Contacts
//Add contact
router.post('/contact', (res, req, next) => {
console.log('Insides');
let newContact = new Contact({
first_name: req.body.first_name,
last_name: req.body.last_name,
phone: req.body.phone
});
newContact.save((err, contact) => {
if (err) {
res.json({
msg: 'Failed to add contact'
});
} else {
res.json({
msg: 'Contact added successfully'
});
}
});
});
// Delete Contacts
router.delete('/contact/:id', (req, res, next) => {
Contact.remove({
_id: req.params.id
}, function(err, result) {
if (err) {
res.json(err);
} else {
res.json(result);
}
});
});
module.exports = router;
//contacts.js
const mongoose = require('mongoose');
const ContactSchema = mongoose.Schema({
first_name: {
type: String,
required: true
},
last_name: {
type: String,
required: true
},
phone: {
type: String,
required: true
}
});
const Contact = module.exports = mongoose.model('Contact', ContactSchema);
Try moving app.use(bodyparser.json()) before app.use('/api', route). Hope this helps.
First of all you need to Declare
//adding middleware
app.use(bodyparser.json());
Before use all routes. So you need to update your app.js like:
//adding middleware
app.use(bodyparser.json());
//routes will happen here
app.use('/api', route);
In additional you need to refactor your route.js
code like this:
//Add contact
router.post('/contact', (res, req, next) => {
const {
first_name,
last_name,
phone
} = req.body;
let newContact = new Contact({
first_name,
last_name,
phone
});
newContact.save((err, contact) => {
if (err) {
res.json({
msg: 'Failed to add contact'
});
} else {
res.json({
msg: 'Contact added successfully'
});
}
});
});
I'm currently trying to get a node.js/express tutorial working (from Express in Action), but haven't been able to access a mongoose model properly. I call the module in a var called "User" I keep getting the error that "User.find is not a function."
Here is the models/user.js file:
var
bcrypt = require("bcrypt-nodejs"),
mongoose = require("mongoose"),
SALT_FACTOR = 10
;
var noop = function() {};
var userSchema = mongoose.Schema({
displayName: String,
bio: String
});
userSchema.pre("save", function(done) {
var user = this;
if (!user.isModified("password")) {
return done();
}
bcrypt.genSalt(SALT_FACTOR, function(err, salt) {
if (err) { return done(err); }
bcrypt.hash(user.password, salt, noop, function(err, hashedPassword) {
if (err) { return done(err); }
user.password = hashedPassword;
done();
});
});
});
userSchema.methods.checkPassword = function(guess, done) {
bcrypt.compare(guess, this.password, function(err, isMatch) {
done(err, isMatch);
});
};
userSchema.methods.name = function() {
return this.displayName || this.username;
};
var User = mongoose.model("User", userSchema);
module.exports = User;
Here is the routes.js file calling it:
var
express = require("express"),
mongoose = require("mongoose"),
flash = require("connect-flash"),
passport = require("passport"),
router = express.Router()
;
var User = ("./models/user");
router.use(function(req, res, next){
res.locals.currentUser = req.user;
res.locals.errors = req.flash("error");
res.locals.infos = req.flash("info");
next();
});
router.get("/", function(req, res, next) {
User.find({}, function(err, users) {
assert.equal(err, null);
res.json(users);
});
});
/*
Original route, also doesn't work
router.get("/", function(req, res, next) {
User.find()
.sort({ createdAt: "descending" })
.exec(function(err, users) {
if (err) { return next(err); }
res.render("index", { users: users });
});
});
*/
module.exports = router;
Lastly here's the index.js file, in case it's relevant
var
http = require("http"),
path = require("path"),
express = require("express"),
flash = require("connect-flash"),
session = require("express-session"),
cookieParser = require("cookie-parser"),
logger = require("morgan"),
liquid = require("shopify-liquid"),
bodyParser = require("body-parser"),
mongoose = require('mongoose')
;
var routes = require('./routes');
var app = express();
mongoose.connect('mongodb://localhost:27017/test');
app.set("port", process.env.PORT || 3000);
var engine = liquid({
root: __dirname, // for layouts and partials
extname: '.liquid'
});
app.engine('liquid', engine.express());
app.set('views', ['./views', './views/partials', './views/layouts']);
app.set('view engine', 'liquid');
var assetsPath = path.resolve(__dirname, "assets");
app.use("/assets", express.static(assetsPath));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
secret: "TKRv0IJs=HYqrvagQ#&!F!%V]Ww/4KiVs$s,<<MX",
resave: true,
saveUninitialized: true
}));
app.use(flash());
app.use(routes);
app.use(logger("dev"));
app.use(function(request, response) {
response.status(404).render("404");
});
http.createServer(app).listen(3000, function(){
console.log('App skeleton started on port 3000.');
});
I've tried the solutions suggested from all similar questions but had no luck.
I'm building an app in NodeJS/Express combined with Mongodb, where I want to be able to comment to a post but I keep getting a 404 not found.
I setup the models and routes in my server.js and also setup the 'ref' between the two but this is the response I keep getting:
And as you can see with the following, the 'capture' aka 'post' does actually exist:
Edit: Made some changes to my initial code with the answers that Zen gave me.
This is my code:
-server.js
// Init Express Web Framework
var express = require('express');
var app = express();
var path = require('path');
// Set view engine to EJS & set views directory
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', path.resolve(__dirname, 'client', 'views'));
app.use(express.static(path.resolve(__dirname, 'client')));
// Database Connection
var mongoose = require('mongoose');
var configDB = require('./server/config/database.js');
require('./server/routes/capture');
require('./server/routes/comment');
mongoose.connect(configDB.url);
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.text());
app.use(bodyParser.json({ type: 'application/json'}));
// Main route
app.get('/', function(req, res){
res.render('index.html');
});
// API
var api = express.Router();
require('./server/routes/capture')(api);
require('./server/routes/comment')(api);
app.use('/api', api);
// Port Settings
app.listen(process.env.PORT || 3000, process.env.IP);
console.log('Listening on port ' + process.env.PORT);
-capture.js model:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var captureSchema = Schema({
birdname: {type: String, required: true},
place: String,
userId: String,
author: String,
picture: Schema.Types.Mixed,
created_at: Date,
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment'}]
});
module.exports = mongoose.model('Capture', captureSchema);
-capture.js route:
var Capture = require('../models/capture');
module.exports = function(router) {
router.post('/captures', function(req, res){
var capture = new Capture();
capture.birdname = req.body.birdname;
capture.place = req.body.place;
capture.userId = req.body.userId;
capture.author = req.body.author;
capture.picture = req.body.picture;
capture.created_at = new Date();
capture.save(function(err, data){
if(err)
throw err;
console.log(req.body);
res.json(data);
});
});
router.get('/captures', function(req, res){
Capture.find({}, function(err, data){
if(err)
throw err;
res.json(data);
});
});
router.delete('/captures', function(req, res){
Capture.remove({}, function(err){
res.json({result: err ? 'error' : 'ok'});
});
});
router.get('/captures/:id', function(req, res){
Capture.findOne({_id: req.params.id}, function(err, data){
if(err)
throw err;
res.json(data);
});
});
router.delete('/captures/:id', function(req, res){
Capture.remove({_id: req.params.id}, function(err){
res.json({result: err ? 'error' : 'ok'});
});
});
};
-capture.js model:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var commentSchema = Schema({
birdname: String,
body: {type: String, required: true},
userId: {type: String, required: true},
author: {type: String, required: true},
created_at: Date,
capture: [{ type: Schema.Types.ObjectId, ref: 'Capture'}]
});
module.exports = mongoose.model('Comment', commentSchema);
-comment.js route:
var Comment = require('../models/comment');
module.exports = function(router) {
router.post('/captures/:capture/comments', function(req, res, next){
var comment = new Comment();
comment.birdname = req.body.birdname;
comment.body = req.body.body;
comment.userId = req.body.userId;
comment.author = req.body.author;
comment.created_at = new Date();
comment.capture = capture;
comment.save(function(err, comment) {
if (err) { return next(err); }
req.capture.comments.push(comment);
req.capture.save(function(err, capture) {
if (err) { return next(err); }
res.json(comment);
});
});
});
};
Any help is much appreciated..
Thanks
I would work with one route script, seeing that your comments are attached to the post.
You also have to add a Map logic to the route parameters for cature and comment.
Try the following:
var Capture = require('../models/capture');
var Comment = require('../models/comment');
module.exports = function(router) {
router.post('/captures', function(req, res){
var capture = new Capture();
capture.birdname = req.body.birdname;
capture.place = req.body.place;
capture.userId = req.body.userId;
capture.author = req.body.author;
capture.picture = req.body.picture;
capture.created_at = new Date();
capture.save(function(err, data){
if(err)
throw err;
console.log(req.body);
res.json(data);
});
});
router.get('/captures', function(req, res){
Capture.find({}, function(err, data){
if(err)
throw err;
res.json(data);
});
});
router.delete('/captures', function(req, res){
Capture.remove({}, function(err){
res.json({result: err ? 'error' : 'ok'});
});
});
// Map logic to route parameter 'capture'
router.param('capture', function(req, res, next, id) {
var query = Capture.findById(id);
query.exec(function (err, capture) {
if (err) { return next(err); }
if (!capture) { return next(new Error("can't find post")); }
req.capture = capture;
return next();
});
});
// Map logic to route parameter 'comment'
router.param('comment', function (req, res, next, id) {
var query = Comment.findById(id);
query.exec(function (err, comment) {
if (err) { return next(err); }
if (!comment) { return next(new Error("can't find comment")); }
req.comment = comment;
return next();
});
});
router.get('/captures/:id', function(req, res){
Capture.findOne({_id: req.params.id}, function(err, data){
if(err)
throw err;
res.json(data);
});
});
router.delete('/captures/:id', function(req, res){
Capture.remove({_id: req.params.id}, function(err){
res.json({result: err ? 'error' : 'ok'});
});
});
router.post('/captures/:capture/comments', function(req, res, next){
var comment = new Comment();
comment.birdname = req.body.birdname;
comment.body = req.body.body;
comment.userId = req.body.userId;
comment.author = req.body.author;
comment.created_at = new Date();
comment.capture = req.capture;
comment.save(function(err, comment) {
if (err) { return next(err); }
req.capture.comments.push(comment);
req.capture.save(function(err, capture) {
if (err) { return next(err); }
res.json(comment);
});
});
});
};
The status code is 404. Apparently, no corresponding router is found. The problem is that you export an "init" function in comment.js route. When you require the comment router in server.js, nothing happens.
// API
var api = express.Router();
require('./server/routes/capture')(api);
// you have forgotten it
require('./server/routes/comment')(api);
app.use('/api', api);
BTW, there is no need putting capture into req.body when you're posting to '/captures/:capture/comments', since you can get :capture by using const capture = req.params.capture;.
The code
app.js:
var express = require('express');
var session = require('express-session');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoStore = require('connect-mongo')(session);
var mongoose = require('mongoose');
var passport = require('passport');
var config = require('./config');
var routes = require('./routes');
var mongodb = mongoose.connect(config.mongodb);
var app = express();
// view engine setup
app.set('views', config.root + '/views');
app.set('view engine', 'jade');
app.engine('html', require('ejs').renderFile);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(express.static(config.root + '/public'));
app.use(session({
name: 'myCookie',
secret: 'tehSecret',
resave: true,
saveUninitialized: true,
unset: 'destroy',
store: new mongoStore({
db: mongodb.connection.db,
collection: 'sessions'
})
}));
app.use(passport.initialize());
app.use(passport.session());
app.use('/', routes);
app.set('port', config.port);
var server = app.listen(app.get('port'), function() {
if (config.debug) {
debug('Express server listening on port ' + server.address().port);
}
});
routes.js:
var express = require('express');
var router = express.Router();
var config = require('../config');
var userController = require('../controllers/user');
var authController = require('../controllers/auth');
router.get('/', function(req, res) {
res.render('index', {
title: config.app.name
});
});
router.route('/users')
.post(userController.postUsers)
.get(authController.isAuthenticated, userController.getUsers);
router.get('/signout', userController.signout);
module.exports = router;
models/user.js:
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var UserSchema = new mongoose.Schema({
username: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
}
});
// Execute before each user.save() call
UserSchema.pre('save', function(callback) {
var user = this;
// Break out if the password hasn't changed
if (!user.isModified('password')) return callback();
// Password changed so we need to hash it
bcrypt.genSalt(5, function(err, salt) {
if (err) return callback(err);
bcrypt.hash(user.password, salt, null, function(err, hash) {
if (err) return callback(err);
user.password = hash;
callback();
});
});
});
UserSchema.methods.verifyPassword = function(password, cb) {
bcrypt.compare(password, this.password, function(err, isMatch) {
if (err) return cb(err);
cb(null, isMatch);
});
};
// Export the Mongoose model
module.exports = mongoose.model('User', UserSchema);
controllers/user.js:
var config = require('../config');
var User = require('../models/user');
exports.postUsers = function(req, res) {
if (config.debug)
console.log("user.postUsers()");
var user = new User({
username: req.body.username,
password: req.body.password
});
user.save(function(err) {
if (err)
return res.send(err);
if (config.debug)
console.log("saved");
res.json({
message: 'New user created!'
});
});
};
exports.getUsers = function(req, res) {
if (config.debug)
console.log("user.getUsers()");
User.find(function(err, users) {
if (err)
return res.send(err);
if (config.debug)
console.log("users", users);
res.json(users);
});
};
exports.signout = function(req, res) {
if (config.debug)
console.log("user.signout()");
res.clearCookie('myCookie');
req.session.destroy(function(err) {
req.logout();
res.redirect('/');
});
};
controllers/auth.js:
var passport = require('passport');
var BasicStrategy = require('passport-http').BasicStrategy;
var config = require('../config');
var User = require('../models/user');
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use(new BasicStrategy(
function(username, password, done) {
User.findOne({
username: username
}, function(err, user) {
if (err) {
return done(err);
}
// No user found with that username
if (!user) {
return done(null, false);
}
// Make sure the password is correct
user.verifyPassword(password, function(err, isMatch) {
if (err) {
return done(err);
}
// Password did not match
if (!isMatch) {
return done(null, false);
}
// Success
return done(null, user);
});
});
}
));
exports.isAuthenticated = passport.authenticate('basic', {
session: false
});
The problem
/signout route does not end the current session. In the req.session.destroy callback the req.session is undefined, yet a new GET request to /users acts like the session is valid.
Can someone help clear this problem out?
If, like me, you came here as a result of question title rather than full details- the answer is req.session.destroy(). I think the logout function is particular to passport.js and will not work if you are using standard express-session.
Solution
controllers/user.js:
exports.signout = function(req, res) {
if (config.debug)
console.log("user.signout()");
req.logout();
res.send(401);
};
Btw. don't mind the session(s) still being in DB immediately after the logout. Mongod checks and clears those out after 60 s.
in sign out api without using req.session.destroy() try req.logout();. I hope it will work.
In my case the server-side code was fine. It was the client-side code where I wasn't including the withCredentials parameter when making the http request.
Below is the correct working code.
// server side (nodejs)
authRouter.post("/logout",
passport.session(),
checkAuthenticationHandler,
async (req, res, next) => {
req.logOut(err => {
if (err) next(err)
res.status(http.statusCodes.NO_CONTENT).end()
})
})
// client side (reactjs)
export const logout = async () => {
const _response = await axios({
method: 'post',
url: `${authApi}/auth/logout`,
withCredentials: true
})
}