connect

Express resources with authentication middleware?

倾然丶 夕夏残阳落幕 提交于 2019-12-18 12:00:06
问题 Passport.js offers great authentication for node.js and Express including a middleware solution: ensureAuthenticated = function(req, res, next) { if (req.isAuthenticated()) { return next(); } return res.redirect("/login"); }; How can I use this middleware in the express-resource module? Unfortunately, app.resource('users', ensureAuthenticated, require('./resources/users')); doesn't work. 回答1: I know this is a little too late, and the original post was answered, however, I was looking for the

How to force parse request body as plain text instead of json in Express?

别来无恙 提交于 2019-12-18 04:28:22
问题 I am using nodejs + Express (v3) like this: app.use(express.bodyParser()); app.route('/some/route', function(req, res) { var text = req.body; // I expect text to be a string but it is a JSON }); I checked the request headers and the content-type is missing. Even if "Content-Type" is "text/plain" it is parsing as a JSON it seems. Is there anyway to tell the middleware to always parse the body as a plain text string instead of json? Earlier versions of req used to have req.rawBody that would

Node.js express correct use of bodyParser middleware

旧巷老猫 提交于 2019-12-17 23:03:05
问题 I am new to node.js and express and have been experimenting with them for a while. Now I am confused with the design of the express framework related to parsing the request body. From the official guide of express: app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(logErrors); app.use(clientErrorHandler); app.use(errorHandler); After setting up all the middleware, then we add the route that we want to handle: app.post('/test', function(req, res){ /

Everyauth vs Passport.js?

笑着哭i 提交于 2019-12-17 21:24:36
问题 Everyauth and Passport.js seem to have very similar feature sets. What are some of the positive and negative comparisons between the two that would make me want to use one over the other? 回答1: Chiming in with my two cents, as the developer of Passport. Before developing Passport, I evaluated everyauth and determined that it didn't meet my requirements. So, I set about implementing a different solution which would. The major points I wanted to address are: Idiomatic Node.js everyauth makes

How do you efficiently connect to mysql in php without reconnecting on every query

霸气de小男生 提交于 2019-12-17 18:38:05
问题 I'm pretty sick of having to rewrite my code every time I learn something new about php (like the fact that mysql connections cannot be passed around in a session as a handle). How do you implement mysql connection in your projects? A lot of people have proposed "connection pooling", but after reading the manual i'm still lost. It's like: "connection pooling is mysql_pconnect!" - me: "and...? how is that any different in reality? can you pass around a mysql_pconnect in a session? why is this

Connect or Express middleware to modify the response.body

丶灬走出姿态 提交于 2019-12-17 11:42:10
问题 I would like to have a middleware function which modifies the response body. This is for an express server. Something like: function modify(req, res, next){ res.on('send', function(){ res.body = res.body + "modified" }); next(); } express.use(modify); I don't understand what event to listen for. Any help or documentation would be appreciate. 回答1: You don't need to listen to any events. Just make it function modify(req, res, next){ res.body = res.body + "modified"; next(); } And use it after

“Cannot GET /” with Connect on Node.js

為{幸葍}努か 提交于 2019-12-17 08:45:24
问题 I'm trying to start serving some static web pages using connect like this: var connect = require("connect"); var nowjs = require("now"); var io = require("socket.io"); var app = connect.createServer( connect.static(__dirname + '/public') ); app.listen(8180); So I added a simple index.html at the /public directory on the same directory as the app.js file is, but when I try to view the page on my browser I get this response from node: Cannot GET / What I'm doing wrong and how I can correct it?

Is it possible to set a base URL for NodeJS app?

时光总嘲笑我的痴心妄想 提交于 2019-12-17 07:09:50
问题 I want to be able to host multiple NodeJS apps under the same domain, without using sub-domains (like google.com/reader instead of images.google.com). The problem is that I'm always typing the first part of the url e.g. "/reader" in Express/NodeJS. How can I set up an Express app so that the base URL is something.com/myapp ? So instead of: app.get("/myapp", function (req, res) { // can be accessed from something.com/myapp }); I can do: // Some set-up app.base = "/myapp" app.get("/", function

Node.js connect only works on localhost

此生再无相见时 提交于 2019-12-17 02:34:29
问题 I've written a small node.js app, using Connect, that serves up a web page, then sends it regular updates. It also accepts and logs user observations to a disk file. It works fine as long as I am on localhost, but I can't get other commputers on the same intranet to see it. I am using port 3000, but changing to port 8080 or 80 didn't help. Here is the code I am using to set up the connection: var io = require('socket.io'), connect = require('connect'); var app = connect().use(connect.static(