express

How can I connect to mongodb using express without mongoose?

半城伤御伤魂 提交于 2020-01-01 03:04:47
问题 I am using the express framework and would like to connect to a mongodb without using mongoose, but with the native nodejs Mongodb driver. How can I do this without creating a new connection every time? To handle get or post requests I currently open a new connection to the db for every request and close it on completion of the request. Is there a better way to do this? Thanks in advance. 回答1: Following the example from my comment, modifying it so that the app handles errors rather than

Why do different node.js sessions share variables?

巧了我就是萌 提交于 2020-01-01 02:25:07
问题 Here is a simple program: var express = require('express'); var app = express.createServer(); var count = 0; app.get("/", function(req, res) { res.send(count.toString()); count++; }); app.listen(3000); When I open it in two different browsers, the first displays 0 and the second displays 1 . Why? They are different sessions so I expect node.js use different child processes for them. My understanding, with PHP, is that sharing variables should be implemented using databases. Why can node.js do

How to redirect to correct client route after social auth with Passport (react, react-router, express, passport)

狂风中的少年 提交于 2020-01-01 00:45:10
问题 I have a React/Redux/React Router front end, Node/Express back end. I’m using Passport (various strategies including Facebook, Google and Github) for authentication. What I want to happen: Unauthenticated user attempts to access protected client route (something like /posts/:postid , and is redirected to /login . (React Router is handling this part) User clicks the ‘Log in with Facebook’ button (or other Social auth service) After authentication, user is automatically redirected back to the

ERR_HTTP_HEADERS_SENT: Cannot set headers after they are sent to the client

南笙酒味 提交于 2019-12-31 21:32:11
问题 I'm facing this weird issue in NodeJS when using with Passport.js, Express and Mongoose. Basically, I get an error saying "Cannot set headers after they are sent to the client" even though I don't send more than one header. I've read other posts and tried them out as well, and none of them worked. app.get - is there any difference between res.send vs return res.send Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client Cannot set headers after they are sent to

What's the better approach: serving static files with Express or nginx?

限于喜欢 提交于 2019-12-31 20:46:46
问题 I'm building a Node.js applications and I'm using nginx as a reverse proxy. My application has some static files I need to serve and a Socket.io server. I know that I can serve static files directly with Express (using express.static middleware). Also I can point nginx directly to the directory where my static files are located, so they would be served by nginx. So, the question: which one is the better approach? Which pros and cons can I face while using each approach? 回答1: for development:

JSON Zip Response in node.js

夙愿已清 提交于 2019-12-31 16:10:20
问题 I'm pretty new to node.js and I'm trying to send back a zip file containing JSON results. I've been trying to figure it out how to do it, but haven't had the expected results. I'm using NodeJS, ExpressJS, LocomotiveJS, Mongoose and MongoDB. Since we're building a mobile oriented application, I'm trying to save as many as bandwith as I can. The daily initial load for the mobile app could be a big JSON document, so I want to zip it before sending it to the device. If possible I'd like to do it

How do you access an Amazon SNS post body with Express / Node.js

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-31 12:54:14
问题 I'm in the process of rebuilding a PHP app in Node.js on top of the Express framework. One part of the application is a callback url that an Amazon SNS notification is posted to. The POST body from SNS is currently read in the following way in PHP (which works): $notification = json_decode(file_get_contents('php://input')); In Express I have tried the following: app.post('/notification/url', function(req, res) { console.log(req.body); }); However, watching the console, this only logs the

How do you access an Amazon SNS post body with Express / Node.js

北战南征 提交于 2019-12-31 12:53:43
问题 I'm in the process of rebuilding a PHP app in Node.js on top of the Express framework. One part of the application is a callback url that an Amazon SNS notification is posted to. The POST body from SNS is currently read in the following way in PHP (which works): $notification = json_decode(file_get_contents('php://input')); In Express I have tried the following: app.post('/notification/url', function(req, res) { console.log(req.body); }); However, watching the console, this only logs the

Password Reset In NodeJS

走远了吗. 提交于 2019-12-31 10:39:21
问题 I have made a set up to update a user's password using NodeJS/Passport. I followed this great guide: http://sahatyalkabov.com/how-to-implement-password-reset-in-nodejs/. 99% of this is working. I had to modify it a bit to include some stripe functionality. I am afraid however I have a critical error somewhere and I cannot find it. The user is currently able to go all the way through the process of having an email sent to them, enter a new password, and be logged in. Another email follows

How to get array of json objects rather than mongoose documents

纵然是瞬间 提交于 2019-12-31 10:07:44
问题 When I do find operation like the follows: Collection.find({name: 'Erik'}, function (err, docs) { // do momething }); 'docs' variable is populated with an array of fully functional mongoose documents. But I need to get an array of pure JSON objects. I know I can loop through the 'docs' array by forEach and get an objects by using .toJSON() method. Does mongoose support the feature I'm interested? 回答1: If you're using Mongoose 3.x you can use the lean query option to do this: Collection.find(