Edit Page

res.send()

Send a string response in a format other than JSON (XML, CSV, plain text, etc.).

This method is used in the underlying implementation of most of the other terminal response methods.

Usage

#
return res.send([string]);

Details

#

This method can be used to send a string of XML.

If no argument is provided, no response body is sent back—just the status code.

Examples

#

To allow users to export their own data, while complying with Europe's GDPR regulations, you might send back some dynamic CSV-formatted data, like this:

// Send back some dynamic CSV-formatted data.
return res.set('text/csv').send(`
some,csv,like,this
or,,like,this
`);

Or, to respond with XML (e.g. for a sitemap):

// Send down some dynamic XML-formatted data.
return res.set('application/xml').send(`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://sailsjs.com</loc>
    <lastmod>2018-03-28T17:02:23.688Z</lastmod>
    <changefreq>monthly</changefreq>
  </url>
</urlset>
`);

You can also send arbitrary plain text and use any status code you like:

// You can use any status code you like.
// (Defaults to 200 unless you specify something else.)
return res.status(420).send('Hello world!');

Notes

#
  • This method is terminal, meaning that it's generally the last line of code your app should run for a given request (hence the advisory usage of return throughout these docs).
  • If you want to send a dictionary or JSON, use res.json().
  • If you want to send a stream, use actions2(preferably) or .pipe(res) (if you absolutely must).
  • If you want to send a custom status code, call req.status() first.

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

Reference