Like i said in title. req.body don't work. I saw some examples and that use req.body(body-parser) well. But in my code, req.body is keep undefined. Also i found same problems and they said "app.use(bodyParser.json())" will work. But for me, no. So i ask how can i solve this error. Plz, help me.
This code is simple, Express and Vue login, signin test project. I think problem is in server file.
Server
var express = require("express");
var bodyParser = require("body-parser");
var path = require("path");
const mongoose = require("mongoose");
var index = require("./routes/index.js");
var login = require("./routes/login.js");
var signin = require("./routes/signin.js");
var app = express();
var http = require("http").Server(app);
mongoose.connect("mongodb://localhost:27017/test");
app.use('/', index);
app.use('/api/login', login);
app.use('/api/signin', signin);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.set("port", process.env.PORT || 3000);
app.use(express.static(path.join(__dirname, 'public')));
http.listen(app.get("port"), function() {
console.log("server start in " + app.get("port"));
})
Router
const express = require("express");
const router = express.Router();
const User = require("../models/user.js");
router.post("/", function(req, res, next) {
const id = req.body.id;
const password = req.body.password;
let info = {
error: "false",
words: "",
id: "",
password: ""
}
User.findOne({ id: id }, function(err, user) {
if(err) {
info.error = "true";
info.words = "Unknown Error Come Out.";
return res.send(info);
}
if(!user) {
info.error = "true";
info.words = "Please Check Your ID or Password";
return res.send(info);
}
info.id = id;
info.password = password;
console.log(info);
return res.send(info);
})
});
module.exports = router;
This body can not be resolved because of your middleware declaration order. You can check from the following blog about middleware execution sequence. Basically it equals to middleware declaration order.
https://derickbailey.com/2016/05/09/in-what-order-does-my-express-js-middleware-execute/
Declare bodyParser middleware before router middleware can solve your problem
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/api/login', login);
Related
I am trying with express vhost but i dont know how to create dynamically that means if a user signup with that username create a subdomain like username.domain.com
my app.js
const express = require("express");
const app = express();
const morgan = require("morgan");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
var cookieParser = require("cookie-parser");
require("dotenv").config({ path: __dirname + "/.env" });
const vhost = require("vhost");
const subdomain = express();
const domain = "localhost";
app.use(vhost(`username.${domain}`, subdomain));
app.use(cookieParser());
subdomain.use(cookieParser());
app.use(morgan("dev"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
subdomain.use(morgan("dev"));
subdomain.use(bodyParser.urlencoded({ extended: false }));
subdomain.use(bodyParser.json());
const userRoutes = require("./api/routes/user");
subdomain.use(userRoutes);
app.use("*", (req, res, next) => {
const error = new Error("Not found");
error.status = 404;
next(error);
});
app.use("*", (error, req, res, next) => {
res.status(error.status || 500);
res.json({
error: {
message: error.message,
},
});
});
module.exports = app;
Here i create subdomain manualy like http://username.localhost:3000/user
I want to create it dynamicaly after user signup using an API
So I am trying to build an authentication system using passport in MERN(MongoDB, Express, React, NodeJS).
I set up everything, I connected react with NodeJS,
The problem is when I am trying to log in or register it shows me,
this error "User.findOne is not a function" in the console and I tried to fix it by looking for any
type mistakes or google it but I didn't find anything online.
Did anyone have a similar mistake and fix it or does anyone know how?
Model Code:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;
const UserSchema = new Schema ({
password: String,
email: String,
}, {
collection: 'information'
})
const User = mongoose.model('information', UserSchema);
module.exports - User;
Passport Code:
const Strategy = require('passport-local').Strategy
const mongoose = require('mongoose')
const User = require('../models/user');
const bcrypt = require('bcryptjs')
const salt = bcrypt.genSaltSync(10);
const SignupStrategy = new Strategy ({ passReqToCallback:true, usernameField: 'email' }, function(req, email, password, done){
User.findOne({email: req.body.email}).lean().exec((err, user) => {
if (err) {
return done(err, null);
}
if (user) {
return done("User already exist", null);
}
const encryptedPassword = bcrypt.hashSync(password, salt);
let newUser = new User({
email,
password: encryptedPassword
})
newUser.save((error, inserted) => {
if (error) {
return done(error, null);
}
delete inserted.password;
return done(null, inserted);
})
})
});
module.exports = SignupStrategy;
Node server:
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const passport = require('./passport/');
const PORT = process.env.PORT || 8080;
const mongoose = require('mongoose');
//I have used password in mongo I just did <password> for security reasons.
const mongoString = mongoose.connect('mongodb+srv://herpryth:<password>#nowyourguest-ga5vy.gcp.mongodb.net/users?retryWrites=true&w=majority', {useNewUrlParser: true})
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/', indexRouter);
app.use('/authentication', usersRouter);
app.use(passport.initialize());
app.use(passport.session());
app.listen(process.env.PORT || 8080, process.env.IP || '0.0.0.0');
module.exports = app
Route:
const express = require('express');
const app = express.Router();
const passport = require('../passport')
app.post('/signup', (req, res, next) =>{
passport.authenticate('local-signup', function(error, user, info){
if (error) {
return res.status(500).json({
message: error || 'Something happend',
error : error.message || "Server error"
});
}
return res.json(user);
})(req, res, next);
})
app.post('/signin', function(req, res, next){
passport.authenticate('local-signin', function(error, user, info){
if (error) {
return res.status(500).json({
message: error || 'Something happend',
error : error.message || "Server error"
});
}
return res.json(user);
})(req, res, next);
})
module.exports = app;
React server:
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const passport = require('./passport/');
const PORT = process.env.PORT || 8080;
const mongoose = require('mongoose');
const mongoString = mongoose.connect('mongodb+srv://herpryth:XFXGJakc18wBJLIk#nowyourguest-ga5vy.gcp.mongodb.net/users?retryWrites=true&w=majority', {useNewUrlParser: true})
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/', indexRouter);
app.use('/authentication', usersRouter);
app.use(passport.initialize());
app.use(passport.session());
app.listen(process.env.PORT || 8080, process.env.IP || '0.0.0.0');
module.exports = app
Does anyone know how to fix this problem?
There is just a simple mistake in your code
replace with
module.exports = User
to
module.exports - User
Getting a 404 error on my POST route, here is what I have done in my auth.js (routes) file:
const express = require('express');
const router = express.Router();
const connection = require('../../helpers/db.js');
const bodyParser = require("body-parser");
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({
extended: true
}));
//create a new user
router.post('/signup', function (req, res) {
const insert = `INSERT INTO users ( email, password, name, lastname) values ('${req.body.email}', '${req.body.password}','${req.body.name}', '${req.body.lastname}' )`
connection.query(insert, function (err, result) {
if(err) {
console.log('mysql error', error);
res.status(500);
res.send(error);
} else {
console.log('a new user was added!');
res.send('Congrats, new user!');
}
})
});
module.exports = router;
Here is my app.js file:
const http = require("http");
const path = require("path");
const express = require("express");
const bodyParser = require("body-parser");
const morgan = require("morgan");
const app = express();
const authRouter = require("./routes/auth/auth");
// Configuring the app
app.use(morgan("dev"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(__dirname + "/public"));
app.use("/signup", authRouter);
//starting node server
const server = app.listen(process.env.PORT || 3001, function(err) {
if(err) throw err;
console.log("Listening on port " + server.address().port);
});
If I change my route into a GET, it works fine, but as soon as I do a POST route it would keep telling me on postman there is a 404 error, been trying many things but im now stuck! Im a beginner here :)
I think you added a prefix to your route so the route is /signup/signup
Try to change this
app.use("/signup", authRouter);
To this
app.use(authRouter);
I am testing an express API with supertest. I am trying to pass in body parameters into the test, as can be seen in the code snippets below, but it appears that the body parameters don't get passed in correctly since I get an error message that the body parameters are undefined.
Running the test with command mocha --recursive returns the following error:
Cannot read property 'email' of undefined
Below is the code from file email-suite.js referencing supertest
'use strict';
var express = require("express");
var bodyParser = require('body-parser');
var mongoose = require("mongoose");
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var supertest = require("supertest");
var chai = require("chai");
var should = chai.should();
var api = require("../server.js");
describe("Email Module", function() {
this.timeout(25000);
before(function(done){
mongoose.createConnection(/* connectionstring */);
mongoose.connection.on('open', function(err) {
if(err) console.log(err);
console.log('connected to server');
});
done();
});
it("Check Email Count", function(done) {
var body = { email: "email#email.com" };
supertest(api)
.post("/emailCount")
.set('Accept', 'application/json')
.send(body) // body is undefined
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) return done(err);
res.body.count.should.equal(2);
done();
});
});
});
Below is the code from file email-api.js
'use strict';
var express = require("express");
var bodyParser = require('body-parser');
var router = express.Router();
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
router.post('/emailCount', function(req, res) {
var email = req.body.email; // req.body is undefined
}
module.exports = router;
Below is the code from the file server.js
var express = require("express");
var app = express();
app.set("port", process.env.PORT || 3000);
var router = require("./user/email-api");
app.use('/', router);
app.listen(app.get("port"), function() {
console.log("App started on port " + app.get("port"));
});
module.exports = app;
Put body-parser always after express object and before every routes in main server file like this
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
//Router task start from here
Other wise always will get undefined since router call first and body parsed later.
Thank you abdulbarik for your answer. I want to add some extra information to aid clarity in case people are still getting undefined values for the request body object, and if (as in my case) your routers and tests are setup differently.
Here is the router that we shall test:
// router.js
const express = require("express");
const router = express.Router();
router.post("/", (req, res) => {
res.json({ success: true, data: req.body });
});
module.exports = router;
The following test code will result in the request body being undefined, and thus the test failing:
// router.test.js
const express = require("express");
const request = require("supertest");
const bodyParser = require("body-parser");
// set up the test app - this will fail
const app = express();
app.use("/routerPath", require("./router")); // this will cause the test to fail, as the router should be setup after the body-paser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// run the test
it("successful response", async () => {
const response = await request(app)
.post("/routerPath")
.send({
firstname: "John",
lastname: "Smith",
})
.set("Accept", "application/json");
expect(response.status).toEqual(200);
expect(response.body).toEqual({
success: true,
data: {
firstname: "John",
lastname: "Smith",
},
});
});
The reason why, as explained by abdulbarik, is that the body-parser code should always be before the router code, so that the parser runs before the router. To make the test pass, simply swap these lines around:
// set up the test app - this will work now
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use("/routerPath", require("./router")); // the router setup should happen after the body-parse setup
I hope that is a helpful clarification.
I am writing a web service in NodeJS using Express. My problem occurs when I want to use the app (Express instance) in other modules. The following configuration works just fine, but if I replace
var app = module.exports = express();
with
var app = express();
and then use
module.exports = app;
at the bottom of app.js, everything will break down. There will be an error when calling app.get() in the auth module (TypeError: app.get is not a function). Can somebody explain to me why it matters where I export the app object?
app.js
var mongoose = require('mongoose');
var express = require('express');
var bodyParser = require('body-parser');
var debug = require('debug')('app');
var morgan = require('morgan');
var config = require('./config');
var app = module.exports = express();
// --- globals ---
app.set('jwtTokenSecret', config.jwtTokenSecret);
// --- middleware ---
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(morgan('dev'));
// --- routes ---
var courses = require('./routes/courses');
var users = require('./routes/users');
var auth = require('./routes/auth');
app.use('/auth', auth);
app.use('/courses', courses);
app.use('/users', users);
// --- database connection ---
mongoose.set('debug', true);
mongoose.connect(config.database, function(err) {
if (err) {
debug('Could not establish connection to mongodb');
} else {
debug('Successfully connected to mongodb');
}
});
routes/auth.js
var express = require('express');
var router = express.Router();
var moment = require('moment');
var jwt = require('jwt-simple');
var User = require("../models/User");
var debug = require('debug')('app');
var app = require("../app");
// POST /auth
router.post('/', function(req, res, next) {
User.findOne({
'username' : req.body.username
}, function(err, user) {
if (err || !user) {
res.status(401).json({ error: "No user found"});
return;
}
if (user.password != req.body.password) {
res.send(401);
}
debug(app.get('database'));
var expires = moment().add(7, 'days').valueOf();
var token = jwt.encode({
user: user.username,
exp: expires
}, app.get('jwtTokenSecret'));
res.json({
success: true,
token: token
});
});
});
module.exports = router;
It's because you have a circular dependency between app.js and auth.js.
In your app.js file, you are requiring auth.js, and inside auth.js, you are requiring app.js. By moving module.exports = app to the bottom of app.js, it is being called AFTER var auth = require('./routes/auth');. This means that when var app = require("../app"); is called inside auth.js, module.exports will not be yet defined inside app.js, hence leading to the error.