express

expressjs bind all routes except 2 folders?

倾然丶 夕夏残阳落幕 提交于 2020-01-03 19:33:11
问题 In expressjs, how do I bind a route to all urls except /static and /fail For example, it sound bind to: /users /books /books/anything but not bind to /static /fail /fail/anything /static/anything 回答1: If you are saying you want to create one route for everything but /static* then here is the command for creating the GET route: app.get(/^((?!\/static).)*$/, function(req, res){ //Do your thing in here... }); 回答2: My question was just slightly different and this was the best question/answer

how to stop middleware chain?

左心房为你撑大大i 提交于 2020-01-03 19:08:16
问题 How can I properly end a middleware chain in case of error ? In my code I have if (someProblem) { res.status(statuscode).json({message: 'oops'}); return; } Yet the midleware in the chain after that keeps getting called giving me the Can't set headers after they are sent error. 回答1: Middleware does not advance on its own. It only goes to the next step in the chain when you call next() so as long as you don't call next() , then the next step will not execute. So, if you just send a response and

I get “Assertion failed: You must include an `id` in a hash passed to `push` ” when querying API

大兔子大兔子 提交于 2020-01-03 18:59:59
问题 I must have read every question on stack overflow regarding my issue and have not found a solution. I am very new to Ember and Node so please bear with me. The server responds with this format: { "_id" : "53fddf59d72f9b4d3a3e164a" "about" : [ {"from" : "foo"}, {"text" : "bar"}, ... ] } My model looks like this: App.About = DS.Model.extend({ from : DS.attr('string'), text : DS.attr('string'), ... } Adapter & serializer: App.ApplicationAdapter = DS.RESTAdapter.extend({ host: 'http://localhost

Unable to call dispose on domain at appropriate time

狂风中的少年 提交于 2020-01-03 18:57:33
问题 I'm having an issue with the domain module. Currently, I'm trying to catch any uncaught errors that are thrown in a request. Using an express middleware and domains. All requests are routed through this function before calling next and moving on to it's proper route. app.use (req, res, next) -> domain = createDomain() domain.on "error", (err) -> res.send(500) domain.dispose() domain.enter() next() The problem is, how do I dispose of the domain if an error is never thrown? I could hoist the

Unable to call dispose on domain at appropriate time

▼魔方 西西 提交于 2020-01-03 18:57:25
问题 I'm having an issue with the domain module. Currently, I'm trying to catch any uncaught errors that are thrown in a request. Using an express middleware and domains. All requests are routed through this function before calling next and moving on to it's proper route. app.use (req, res, next) -> domain = createDomain() domain.on "error", (err) -> res.send(500) domain.dispose() domain.enter() next() The problem is, how do I dispose of the domain if an error is never thrown? I could hoist the

Cannot find module 'ejs' - Node.js app on Heroku

寵の児 提交于 2020-01-03 18:51:48
问题 My app was working locally but when I deployed to Heroku, there was an issue. This was my error, It seems that the error was caused by something that I failed to do after I added EJS. 2014-01-06T16:53:39.285571+00:00 app[web.1]: module.js:340 2014-01-06T16:53:39.285956+00:00 app[web.1]: throw err; 2014-01-06T16:53:39.286233+00:00 app[web.1]: ^ 2014-01-06T16:53:39.288439+00:00 app[web.1]: Error: Cannot find module 'ejs' 2014-01-06T16:53:39.288439+00:00 app[web.1]: at Function.Module.

Express.js 4 - use middleware for authentication before static files

雨燕双飞 提交于 2020-01-03 18:51:09
问题 In my express app I've set static files to be served from the /public directory with this line: app.use(express.static(__dirname + '/public')); Now I need to add a middleware for authentication before serving the static content and if the user is not authenticated to be redirected to a route for authentication (e.g., /login ). I'm not really sure how I have to do it. Any ideas? 回答1: Since you didn't specify it, I'm going to assume that you already have some kind of authentication system. In

Cannot find module 'ejs' - Node.js app on Heroku

泄露秘密 提交于 2020-01-03 18:50:08
问题 My app was working locally but when I deployed to Heroku, there was an issue. This was my error, It seems that the error was caused by something that I failed to do after I added EJS. 2014-01-06T16:53:39.285571+00:00 app[web.1]: module.js:340 2014-01-06T16:53:39.285956+00:00 app[web.1]: throw err; 2014-01-06T16:53:39.286233+00:00 app[web.1]: ^ 2014-01-06T16:53:39.288439+00:00 app[web.1]: Error: Cannot find module 'ejs' 2014-01-06T16:53:39.288439+00:00 app[web.1]: at Function.Module.

socket.on event gets triggered multiple times

我们两清 提交于 2020-01-03 18:42:28
问题 var express = require('express'); var app = express(); var server = app.listen(3000); var replyFromBot; app.use(express.static('public')); var socket = require('socket.io'); var io = socket(server); io.sockets.on('connection' , newConnection); function newConnection(socket) { console.log(socket.id); listen = true; socket.on('Quest' ,reply); function reply(data) { replyFromBot = bot.reply("local-user", data); console.log(socket.id+ " "+replyFromBot); socket.emit('Ans' , replyFromBot); } } i've

Express 4 middleware when route is not found (finalhandler not called): how to check for it?

倾然丶 夕夏残阳落幕 提交于 2020-01-03 16:55:09
问题 I set up the following Express 4 middleware stack: const app = express(); const router = express.Router(); router.get('/test', testResponse); app.use(checkAccessToken); app.use(router); app.use(sendResponse); //Error Handling app.use(function(err,req,res,next) { // ... Do something here }); function sendResponse(req, res) { res.json({ data: res.locals.data, meta: res.locals.meta }); } If I call the server with a route that doesn't exist (like GET /something) the function sendResponse just