javascript - Node Express gives two responses, breaks front-end script -


due in express routing, data seems sent twice angular front-end. seems send mixpanel endless loop causes parts of app not load properly.

the reason following code want detect user agents , send them other regular user sees. because user agents won't load javascript, need them scrape information page.

from server.js:

app = express();  app      .use( morgan( 'dev' ) )     .use(bodyparser.urlencoded( { limit: '50mb', extended: true } ) )     .use( bodyparser.json( { limit: '50mb' } ) )     .use( '/api', require('./routes/usersroute.js') )     .use( express.static( indexpath ) )     .all( '/*', require( './routes/home.js' ) ) 

from home.js:

router.get( '/:user/:stream/:slug', function( req, res, next ) {      if ( req.headers['user-agent'].indexof( 'facebook' ) != -1 ) {                     if ( !req.params.user && !req.params.stream && !req.params.slug ) return next()          contentcontroller.finduserid( req.params.user )         .then( function ( userid ) {             if ( !userid ) return next()              contentcontroller.projectcontent( req.params.slug )             .then( function ( item ) {                 if ( !item ) return next()                  createopengraph( item[0] )                 .then( function ( og ) {                     return res.status( 200 ).send( og )                 })                 .catch( function ( error ) {                     console.log( error )                     return res.status( 500 ).json( error )                 })             })         })     } else {          return res.status( 200 )             .set( { 'content-type': 'text/html; charset=utf-8' } )             .sendfile( indexpath )     } })  router.get( '/*', function( req, res ) {     return res.status( 200 )         .set( { 'content-type': 'text/html; charset=utf-8' } )         .sendfile( indexpath ) }) 

a few pages seem broken, , i'm not sure why pages break not others. notably, page trying detect in .get( '/:user/:stream/:slug'...) function in home.js breaks. on page, in console, can see error angular loaded twice, corroborates mixpanel breaking due double response problem. error doesn't appear on pages don't break. further more, can see both functions (in home.js) being called each time load on broken page, not working ones.

i'm open advice on how better.

you have defined routes on routes/home.js file call before other matching route. need pass control next route executing next parameter. want send file on route, perhaps on .use( '/app', require('./routes/approutes.js') ) or on last method on example.

router.get( '/:user/:stream/:slug', function( req, res, next ) {    if ( req.headers['user-agent'].indexof( 'facebook' ) != -1 ) {     /*do facebook stuff*/   } else {       res.status( 200 );   }   next();  }); 

// routes/approutes.js

    router.get( '/*', function( req, res, next ) {         return res.status( 200 )         /*set headers independently on api routes might prefer set content type application/json*/          .set( { 'content-type': 'text/html; charset=utf-8' } )          .sendfile(indexpath ).end();     }); 

// server.js

app = express();  app.use( morgan( 'dev' ) )     .use(bodyparser.urlencoded( { limit: '50mb', extended: true } ) )     .use( bodyparser.json( { limit: '50mb' } ) )     .use( '/api', require('./routes/usersroute.js') )     .use( '/app', require('./routes/approutes.js') )     .use( express.static( indexpath ) )     .all( '/*', require( './routes/home.js' ) ); 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -