express

How to keep alive an NodeJS Passport session

馋奶兔 提交于 2020-01-22 18:56:27
问题 We're Node + Express + Passport for authentication, and persisting the session info to Redis. I have maxAge set on the session cookie, to time out in one hour. That all seems to be working fine but the problem is, the session cookie will expire in one hour regardless of the user's activity. Is there a way I can manually refresh/keep alive the session cookie? 回答1: You'll likely want to use the "rolling" option for your session. This "forces a cookie set on every response" and "resets the

Error: req.flash() requires sessions

瘦欲@ 提交于 2020-01-22 18:49:24
问题 I'm new to node and I'm pretty sure I've set up the middle ware and express to use flash messaging however I still get the error: Error: req.flash() requires sessions Setup //express.js var flash = require('connect-flash') module.exports = function (app, config, passport) { app.use(flash()); }; //route js exports.loginGet = function (req, res) { res.render('users/login', { title: 'Login', message: req.flash('error') //error in question }); }; What else can I do to make sure I have everything

Render raw html in response with Express

时光怂恿深爱的人放手 提交于 2020-01-22 17:55:06
问题 I would like to know how to render a raw HTML string in a response with Express. My question is different from the others because I am not trying to render a raw HTML template; rather I just want to render a single raw HTML string. Here is what I have tried in my route file. router.get('/myRoute', function (req, res, next) { var someHTML = "<a href=\"foo\">bar</a>" res.end(someHTML); }); But when I point my browser to this route, I see a hyperlink, instead of a raw HTML string. I have tried

How to retrieve client and server IP address and port number in Node.js

青春壹個敷衍的年華 提交于 2020-01-22 17:30:47
问题 I tried to search a lot to find a way to know the client and server ip address and port number. So far I found: Client ip : can be known by req.ip . Client port : I searched a lot but I couldn't find any way to find this client ephemeral port. After inspecting the req and res objects, I found that sometimes res.connection._peername contains the client ip address and port number. But this is not the reliable way to find the port number because in some request this property is missing. So, what

How to send parameters to the middleware functions in Express routing?

最后都变了- 提交于 2020-01-22 15:34:05
问题 I am working on a MEAN project where we have some defined api routes like this: //products.controller.js var express = require('express'); var router = express.Router(); const m = require('../../middlewares'); router.get('/products', [m.functionA, m.functionB, m.functionC], getProducts); router.post('/products', [m.functionA, m.functionB, m.functionC], addNewProduct); module.exports = router; function getProducts(req, res) { //code } function addNewProduct(req, res) { //code } ..............

Allow multiple CORS domain in express js

大兔子大兔子 提交于 2020-01-22 14:10:12
问题 How do I allow multiple domains for CORS in express in a simplified way. I have cors: { origin: "www.one.com"; } app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", cors.origin); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); }); This works when there is only one domain mentioned in origin But if I want to have origin as an array of domains and I want to allow CORS for all the domains in the origin array, I

how to recover from duplicate key in mongoose + express

我是研究僧i 提交于 2020-01-22 13:59:12
问题 I've created a signup form using mongoose and express 3 Its possible the user already exists with that username, in which case I get an err.code 11000 (duplicate key). How should I handle existing users? This is what I'm doing now....but I'm not sure checking error code is best way: user.save(function(err){ if ( err ) { console.log(err); console.log(err.code); //duplicate key if ( err.code == 11000 ) { req.flash('error', 'User already exists'); res.redirect('/signup'); return; } } res.locals

Specifying Mongo Query Parameters From Client Controller (MEAN.JS)

允我心安 提交于 2020-01-22 12:19:44
问题 I am building an application using MongoDB, Angular, Express, and Node (MEAN stack). I used the MEAN.JS generator to scaffold my application. I will use the articles module as a reference. Suppose I have 7000 records in my articles collection, and each record has a date associated with it. It is inefficient to load all 7000 records into memory every time I load the page to view the records in a table and I am seeing terrible performance losses because of it. For this reason, I would only like

Centralizing error handling in an express.js-based app

二次信任 提交于 2020-01-22 10:04:43
问题 I just recently started working on an express.js based application, which also uses the pg module (https://github.com/brianc/node-postgres) I also spent a significant amount of time, reading about node and express approach error handling, the benefits of properly designing middleware, etc. Yet, a recurring problem is still buzzing me without a solution. Say, I have the following router method: app.get("/:someThing/:someId", function(req, res, next) { pgClient.query("some SQL query", function

CSS file blocked: MIME type mismatch (X-Content-Type-Options: nosniff)

安稳与你 提交于 2020-01-22 09:01:08
问题 I am developing an Angular 4 app and I want to apply some global styles. Following the tutorial at the angular site, I've created a "styles.css" file in the root directory of my app, and I'm referring to that stylesheet in the index.html of my app: <link rel="stylesheet" type="text/css" href="styles.css"> The angular app is successfully compiled: $ ng serve ** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200 ** [...] webpack: Compiled