express-4

Express middleware cannot trap errors thrown by async/await, but why?

和自甴很熟 提交于 2019-12-10 14:45:08
问题 These two middleware functions behave differently and I cannot figure out why: Here, the error will get trapped by try/catch: router.get('/force_async_error/0', async function (req, res, next) { try{ await Promise.reject(new Error('my zoom 0')); } catch(err){ next(err); } }); But here, the error will not get trapped by try/catch: router.get('/force_async_error/1', async function (req, res, next) { await Promise.reject(new Error('my zoom 1')); }); I thought Express wrapped all middleware

Express insert values into MySQL database

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 09:53:38
问题 PROBLEM SOLVED. Please look below. I'm new into Express and NodeJS, ditched Laravel and PHP. What I want to do is to be able to add a record into MySQL database, but I am not able to connect the dots. I'm following this tutorial series : http://eddyjs.com/bookshelf-js/ http://eddyjs.com/using-mysql-with-bookshelf-js-part-2-using-the-database/ There are two db variables, I couldn't understand how to use them. Here's the error. Cannot read property 'extend' of undefined TypeError: Cannot read

Express insert values into MySQL database

江枫思渺然 提交于 2019-12-06 04:51:03
PROBLEM SOLVED. Please look below. I'm new into Express and NodeJS, ditched Laravel and PHP. What I want to do is to be able to add a record into MySQL database, but I am not able to connect the dots. I'm following this tutorial series : http://eddyjs.com/bookshelf-js/ http://eddyjs.com/using-mysql-with-bookshelf-js-part-2-using-the-database/ There are two db variables, I couldn't understand how to use them. Here's the error. Cannot read property 'extend' of undefined TypeError: Cannot read property 'extend' of undefined at Object. (/Applications/MAMP/htdocs/myapp/myapp/models/User.js:5:20) at

Node Express 4 middleware after routes

元气小坏坏 提交于 2019-12-05 08:40:12
问题 Following the upgrade to Express 4, and the removal of app.router, I'm struggling to get middleware to execute after routes execute. e.g. the following code correctly responds with "hello", but never calls the configured middleware var express = require( "express" )(); express.get( "/", function( req, res ) { res.send( "hello" ); } ); express.use( function( req, res, next ) { console.log( "world" ); next(); } ); express.listen( 8888 ); CLARIFICATION: the following code shows "before" on the

Node Express 4 middleware after routes

坚强是说给别人听的谎言 提交于 2019-12-03 22:07:23
Following the upgrade to Express 4, and the removal of app.router, I'm struggling to get middleware to execute after routes execute. e.g. the following code correctly responds with "hello", but never calls the configured middleware var express = require( "express" )(); express.get( "/", function( req, res ) { res.send( "hello" ); } ); express.use( function( req, res, next ) { console.log( "world" ); next(); } ); express.listen( 8888 ); CLARIFICATION: the following code shows "before" on the console, but not "after": var express = require( "express" )(); express.use( function( req, res, next )

Node.js use csurf conditionally with express 4

时光毁灭记忆、已成空白 提交于 2019-12-03 14:11:06
I try to use csurf on just a few routes in my express app. that's the approach: var express = require('express'); var session = require('express-session'); var csrf = require('csurf'); // some more stuff var csrfExclusion = ['/myApi','/unsecure']; var app = express(); var conditionalCSRF = function (req, res, next) { if (csrfExclusion.indexOf(req.path) !== -1){ next(); } else{ csrf(); } }); app.use(conditionalCSRF); even tried: var conditionalCSRF = function (req, res, next) { if (csrfExclusion.indexOf(req.path) !== -1){ next(); } else{ csrf(req, res, next); next(); } }); and var

Elasticsearch show all results using scroll in node js

北战南征 提交于 2019-12-02 23:57:58
I am basically trying to show all records of an index type. Now, if you use match_all() in query elasticsearch shows 10 results by default. One can show all results using scroll. I am trying to implement scroll api, but can't get it to work. It is showing only 10 results, my code: module.exports.searchAll = function (searchData, callback) { client.search({ index: 'test', type: 'records', scroll: '10s', //search_type: 'scan', //if I use search_type then it requires size otherwise it shows 0 result body: { query: { "match_all": {} } } }, function (err, resp) { client.scroll({ scrollId: resp.

Express 4, NodeJS, AngularJS routing

让人想犯罪 __ 提交于 2019-11-30 09:20:52
问题 I am using Express 4 to host my AngularJS app on my backend, with Nginx as my frontend server. However html5 mode does not seem to work, as I will get a Cannot /GET error when I try to enter the page link (e.g. http://localhost/login ) via the browser. Is there any routing configuration I need to do for my Express/Nginx? Here's my config code: Express 4: var express = require('express'), app = express(), server = require('http').Server(app), bodyParser = require('body-parser'), db = require('

How to handle FormData from express 4

旧时模样 提交于 2019-11-27 19:42:26
I tried sending some form data to my node server but req.body has none of my form fields the node side var express = require('express') var app = express() var path = require('path') var bodyParser = require('body-parser') app.use(bodyParser.urlencoded({ extended: true })); app.get('/', function (req, res) { res.sendFile('index.html') }) app.post('/sendmail', function (req, res) { const formData = req.body.formData this is what I'm sending from the browser fetch('/send', { method: 'POST', body: new FormData(form) }) in dev tools I only see the data passed in the Referer, maybe that is my issue

Express 4 + pm2 watch not working

依然范特西╮ 提交于 2019-11-27 18:01:45
I'm running pm2 with this: pm2 start bin/www --watch ../ Problem is that when I update app.js in the root folder, it doesn't seem to be autorestarting node. Any ideas? Figured out a solution: //processes.json: { "apps" : [{ "name" : "someExpress4App", "script" : "bin/www", "watch" : "../", "log_date_format" : "YYYY-MM-DD HH:mm Z", }] } Put that on the root of your project, then run your pm2 as so: pm2 start processes.json 来源: https://stackoverflow.com/questions/27882759/express-4-pm2-watch-not-working