46976

ExpressJS / NodeJS Redirect Image

Question:

Is it okay for an img to have src value with redirect to another page?

In view, I have img:

<img src='/images/fileName'/>

In app.js

app.get('/images/:fileName', subject.image);

This is my route:

exports.image = function(req, res){ var fileName = req.fileName; fs.exists(fileName, function(exists){ if (exists) { res.sendfile(imagePath); }else{ res.redirect('http://gravatar.com/avatar/_some_hash'); } }); }

My code works, but is it okay to use res.redirect if image does not exist?

Thanks

Answer1:

Yes, it's ok to redirect. In the end its just a GET to the HTTP server and the browser will follow the redirect.

As mentioned in this question: (<a href="https://stackoverflow.com/questions/3778347/is-it-ok-to-http-redirect-images" rel="nofollow">Is it OK to HTTP redirect images?</a>) you might not want do to that for lots of images.

Answer2:

Also You may set default image:

<img src="/images/fileName" onerror="this.src='/images/default.png'" />

Recommend

  • angularjs - using 'string' names on the scope or ng-model
  • How to setup the “Loading Data from a Server” example on the EmberJS homepage within an Ember CLI st
  • Socket.io emit to specific client in tabs
  • Using a promise for retrieving data inside angular directive
  • Global session variable in express.js route?
  • parsing xml and html page with lxml and requests package in python
  • JSON Error when parsing “… has no method 'replace'”
  • Express JS Display Data By ID
  • Can't resolve 'jquery' with typescript
  • Angular page doesn't refresh after data is added or removed
  • How to map Request parameter in Spring?
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • Relative paths. baseUrl and paths not working on ionic2 - angular2
  • Python getting common name from URL using ssl.getpeercert()
  • CORS with socket.io
  • How to resolve permission denied maybe missing internet permission?
  • passing a default argument to a browserify module
  • Laravel 4 routing not working due to .htaccess file?
  • Iron Router: How do I send data to the layout?
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • Code in Job's Script Block after Start-Process Does not Execute
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Python urlparse: small issue
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • FileReader+canvas image loading problem
  • Volusion's generic SQL folder, functionality
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Microsoft Visual Studio Community 2015 always crashes in Windows 10 if swithed to Visual FoxPro
  • How to delete a row from a dynamic generate table using jquery?
  • Proper way to use connect-multiparty with express.js?
  • using HTMLImports.whenReady not working in chrome
  • Authorize attributes not working in MVC 4
  • python draw pie shapes with colour filled
  • EntityFramework adding new object to nested object collection
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?