express

error: SyntaxError: Unexpected token B in JSON at position 0

白昼怎懂夜的黑 提交于 2020-01-06 08:08:55
问题 I am trying to set up a user register form using react and then sending the inputs to the express backend api to register the new user. I keep gettting the error: Register.js:19 POST http://localhost:3000/api/register 400 (Bad Request) and: error: SyntaxError: Unexpected token B in JSON at position 0 on the client side. On the server side I try and console.log(req.body) and only an empty object gets returned. My react register code import React from 'react' class Register extends React

No request from form in node js

安稳与你 提交于 2020-01-06 08:06:14
问题 I'm working with node js, express and handlebars with two forms. The first form is for doing the login and the second one for registering the new user. They are almost similar but the first one is working and the second one, no and I don't know why. This is the code from register.hbr <div class="login-page"> <div class="form"> <form class="login-form" name="register" method="POST" action="/register_success"> <input type="text" placeholder="email" required/> <input type="password" placeholder=

knowing better authentication with Passport / JwtStrategy

送分小仙女□ 提交于 2020-01-06 07:18:16
问题 On one working project I downloaded from internet... In one location of the code I have the following: passport.use(new JwtStrategy({ secretOrKey: credentials.secret, jwtFromRequest: ExtractJwt.fromAuthHeader(), }, function(payload, done) { User.findById( payload._id, function(err, user) { if (err) { return done(err, false); } if (user) { return done(null, user); } else { return done(null, false); } } ); } )); In other location of the code I have the following: var requireAuth = passport

How to access raw body of a post request in Express.js?

你离开我真会死。 提交于 2020-01-06 07:16:31
问题 I send this request with postman. And I console.log(req.body) returns an array like this: { '{"urls":["https://example.com?paramOne': 'foo', paramTwo: 'bar"]}' } How can I get the whole body as a simple string like this? {"urls":["https://example.com?paramOne=foo&paramTwo=bar"]} 回答1: In app.js : Replace: app.use(express.json()); With: var rawBodySaver = function (req, res, buf, encoding) { if (buf && buf.length) { req.rawBody = buf.toString(encoding || 'utf8'); } } app.use(bodyParser.json({

How to return current user information from MongoDB in a MEVN stack app

旧巷老猫 提交于 2020-01-06 06:59:14
问题 I am attempting to build a basic login/registration app using Vue, Express, Node, and MongoDB. I have been successful in setting up the Express router to enable registration and logging in of users, with basic user info stored in MongoDB. I am attempting to return user data back to the screen after logging in. So far, I have set up router.get() in Express to return the username of all users back to the screen. However, I want to configure the axios.get() method in Vue.js to return only the

How to return current user information from MongoDB in a MEVN stack app

两盒软妹~` 提交于 2020-01-06 06:58:59
问题 I am attempting to build a basic login/registration app using Vue, Express, Node, and MongoDB. I have been successful in setting up the Express router to enable registration and logging in of users, with basic user info stored in MongoDB. I am attempting to return user data back to the screen after logging in. So far, I have set up router.get() in Express to return the username of all users back to the screen. However, I want to configure the axios.get() method in Vue.js to return only the

Create Mongoose collection on Express

橙三吉。 提交于 2020-01-06 06:58:14
问题 On app.js, I have this: let Gamestat = require('./models/gamestat'); mongoose.connect(config.database, { useMongoClient: true }); let db = mongoose.connection; io.on('connection', (socket) => { (...) socket.on('gameEnded', (data) => { socket.broadcast.to(data.room).emit('gameEnd', data); //add game stats to database let newStat = new Gamestat({ time: 'testing', winner: 'testing', loser: 'testing', moves: 'testing' }); }); }); /config/database.js: module.exports = { database: 'mongodb:/

Create Mongoose collection on Express

最后都变了- 提交于 2020-01-06 06:56:37
问题 On app.js, I have this: let Gamestat = require('./models/gamestat'); mongoose.connect(config.database, { useMongoClient: true }); let db = mongoose.connection; io.on('connection', (socket) => { (...) socket.on('gameEnded', (data) => { socket.broadcast.to(data.room).emit('gameEnd', data); //add game stats to database let newStat = new Gamestat({ time: 'testing', winner: 'testing', loser: 'testing', moves: 'testing' }); }); }); /config/database.js: module.exports = { database: 'mongodb:/

Internal Error 500 at POST request Express JS and Formidable

。_饼干妹妹 提交于 2020-01-06 06:54:16
问题 I want to send form data (multipart) to my express js server. I learned I could use multer or formidable. Formidable seemed easy to understand for me So I went with it, but I am having issue I need guidance with. I keep getting this error 500 internal server and I just can't figure out what am I doing wrong. Here is my app.js 'use strict'; var debug = require('debug'); var express = require('express'); var fs = require('fs'); var path = require('path'); var favicon = require('serve-favicon');

Internal Error 500 at POST request Express JS and Formidable

和自甴很熟 提交于 2020-01-06 06:54:09
问题 I want to send form data (multipart) to my express js server. I learned I could use multer or formidable. Formidable seemed easy to understand for me So I went with it, but I am having issue I need guidance with. I keep getting this error 500 internal server and I just can't figure out what am I doing wrong. Here is my app.js 'use strict'; var debug = require('debug'); var express = require('express'); var fs = require('fs'); var path = require('path'); var favicon = require('serve-favicon');