Hello gus whats the difference between usingvar response = await axios.get("/api"); // OR axios .get("/api") .then(res => { response = res; }) .catch(e => { response = null; }); When using the first one when the request fails what happen with the response variable? i just need to make an if statement like that:if(response){ //do something } // OR if (response.data){ // do something } // To know if an error happened what i should do? if(response){ }else{ // there's an error here?? } Thank you guys.
Submitted February 19, 2019 at 10:01AM by ayech0x2
Tuesday, 19 February 2019
Build A Group-Chat App in 30 Lines Using Node.js
https://medium.com/@dkhd/15bfe7a2417b
Submitted February 19, 2019 at 08:51AM by jsloverr
Submitted February 19, 2019 at 08:51AM by jsloverr
How to create an IMAGE MANAGER with VueJS & NodeJS - Day 18 - #100DaysOf...
https://www.youtube.com/attribution_link?a=cMSK77csy4Y&u=%2Fwatch%3Fv%3DnXUWsPhQcWQ%26feature%3Dshare
Submitted February 19, 2019 at 09:06AM by Tyler_Potts_
Submitted February 19, 2019 at 09:06AM by Tyler_Potts_
Monday, 18 February 2019
Hi, Can anyone tell me what happens to the pending promise in node architecture?
The single thread will be used by other process but what happen to the previous one? Where will it go?
Submitted February 19, 2019 at 06:33AM by Knightash
Submitted February 19, 2019 at 06:33AM by Knightash
Using fs.watchFile(), how can I get which data was changed?
I hope the title is self explanatory. If I have 'x' in the file, and write 'y' to the file, how can I return that 'y' was added? I'm not sure sure how else to word it. Thanks in advance
Submitted February 19, 2019 at 05:19AM by Vezqi
Submitted February 19, 2019 at 05:19AM by Vezqi
Deno v0.3.0 released
https://github.com/denoland/deno/blob/master/Releases.md
Submitted February 19, 2019 at 05:08AM by kevinkassimo
Submitted February 19, 2019 at 05:08AM by kevinkassimo
After using passport.authenticate(), my express server routes all stop responding
I'm creating a small demo app to learn user authentication with passport.js, sequelize & express. When I hit my /register endpoint, it creates a user account just fine. Same with if I hit my /logIn endpoint, it'll return the proper user if I give it the correct credentials.The issue I've run into is that after starting the server up and hitting either the /register or /logIns endpoint the server will stop responding to all GET and POST requests until I restart the server.Here is my index.js code:var express = require('express') var bodyParser = require('body-parser') var Sequelize = require('sequelize') var session = require('express-session') var passport = require('passport') var bCrypt = require('bcrypt') var LocalStrategy = require('passport-local').Strategy var flash = require('connect-flash') var morgan = require('morgan') var app = express() app.use(express.static('public')) app.use(bodyParser.json()) app.use(session({ secret: 'keyboard cat' })) app.use(passport.initialize()) app.use(passport.session()) app.use(flash()) // Sequelize & Sequelize models var sequelize = new Sequelize('auth_demo', 'auth_demo', 'auth_demo', { host: 'localhost', dialect: 'mysql' }) var User = sequelize.define('user', { id: { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true }, username: { type: Sequelize.STRING }, password: { type: Sequelize.STRING } }) // Server initialization stuff require('./config/passport.js')(passport, User) sequelize .sync() .then(() => { console.log('Connection successfully established') }) .catch(err => { console.log('Unabled to connect:', err) }) app.listen(3000, () => { console.log('App is running on port 3000') }) // Routes. app.post('/register', passport.authenticate('local-signup'), function(req, res) { res.send(req.user) }) app.post('/logIn', function(req, res, next) { passport.authenticate('local-signin', function(err, user, info) { if (err) { console.log("found err") return next(err) } if (!user) { console.log("didnt find user") return res.redirect('/') } req.logIn(user, function(err) { console.log("executing login") if (err) { return next(err) } console.log(req) return res.send(user) }) })(req, res, next) }) app.get('/loggedInUser', function(req, res) { console.log(req.user) res.send(req.user) }) app.get('/test', (req, res) => { console.log(req) res.send('Hello') }) Also, here is the passport.js file to show the strategies:const LocalStrategy = require('passport-local').Strategy const bCrypt = require('bcrypt') module.exports = function(passport, User) { // Passport stuff passport.serializeUser(function(user, done) { done(null, user.id) }) passport.deserializeUser(function(user, done) { User.findById(user.id, function(err,user) { done(err, user) }) }) // Configure passport strategy passport.use('local-signup', new LocalStrategy({ usernameField: 'username', passwordField: 'password' }, function (username, password, done) { var generateHash = function(password) { return bCrypt.hashSync(password, bCrypt.genSaltSync(8), null) } User.findOne({ where: { username: username } }).then(function(user) { if (user) { return done(null, false, { message: 'That user already exists' }) } else { console.log("Creating user") var userPassword = generateHash(password) User.create({ username: username, password: userPassword }) .then(function(newUser) { if (!newUser) { return done(null, false) } if (newUser) { return done(null, newUser) } }) } }) } )) passport.use('local-signin', new LocalStrategy({ usernameField: 'username', passwordField: 'password', passReqToCallback: true }, function (req, username, password, done) { var isValidPassword = function(userpass, password) { return bCrypt.compareSync(password, userpass) } User.findOne({ where: { username: username } }).then(function(user) { if (!user) { console.log("didnt find user") return done(null, false, { message: 'User does not exist' }) } if (!isValidPassword(user.password, password)) { console.log("bad password") return done(null, false, { message: 'Incorrect password' }) } return done(null, user) }) .catch(function(err) { console.log("Error:", err) return done(null, false, { message: 'Something went wrong with your signin' }) }) } )) } If I hit that /test endpoint before I try to hit a /logIn endpoint or /register endpoint, it works fine and prints "hello" as expected. However, if I hit the /logIn or /register endpoint right after starting the server up and then try to hit /test the request will hang and not go anywhere at all.Here is what the express debugger prints in the terminal when it hangs: express:router dispatching GET /test +4s express:router query : /test +1ms express:router expressInit : /test +0ms express:router serveStatic : /test +0ms express:router jsonParser : /test +2ms express:router session : /test +0ms express:router initialize : /test +0ms express:router authenticate : /test +0ms It stops at express:router authenticate, so I'm inclined to believe it's passport.Any help is appreciated, thank you.
Submitted February 19, 2019 at 01:16AM by macswaggerrrr
Submitted February 19, 2019 at 01:16AM by macswaggerrrr
Subscribe to:
Posts (Atom)