The express send, or res.send method can be used to send a string or object when it comes to making very simple basic express middleware methods that respond to incoming client requests. It is not always the best tool for the job though and in some situations it should at least be used in conjunction with other express app.methods. So this will be a quick post on the res.send method, and related topics.
1 - Express send, and what to know
2 - Express send string
For the most part the res.send method is used to just send a simple string message when it comes to very simple, basic express.js examples. However it is not such a bad choice when it comes to making a far more advanced project with express as well. It is just that there are of course alternatives. For example if I am doing something with ejs templates I would often use res.render in conjunction with an ejs template file, and an object that contains data that will be used in that template.
However if I do just simply want to send a plain text string as a response to a request then of course the res.send method gets the job done just fine.
There is often more to a response then just the content that is sent though. In some cases I might want to set a status header before hand, and also the question of sending objects and files as well. In some cases the res.send method can still be used, just not by itself. In other cases another method should really be used over res.send as well, so lets continue with some more examples.
3 - Express send Number
If the data type that is being sent is a number rather than a string that will end up being considered a status code by res.send. Using res.send in this matter is not a good idea. In the 4.x version that I was using for this post a message was spit out in the console informing me that using res.send in this matter is depreciated, and that res.sendStatus should be used in place of that.
The res.sendStatus method will work fine for this, but another option would be to use the res.status method in conjunction with res.send, or better yet res.render when it comes to making more interesting and useful status pages for things like 404, 403, and so forth.
If I do just want to send a number as the content then that should be converted to a string like in the above example.
4 - Express send Object
When sending an object of any kind that will result in JSON data being sent to the client.