javascript - Having a fixed response structure with node.js and Express -
we have started using node.js our api server instead of java. apart things node.js provides, 1 thing miss having proper response object api.
since javascript being dynamically typed languages, objects can created on fly while returning response. in contrast java, can have class , instance of serialized in response. can anytime lookup class determine response of api be.
is there such design pattern in node.js / javascript. our api's have strict conformance such templated object.
you can make them yourself.
if you're using es6 example, can have various error , response modules, , perform own validation in class creates responses.
for example,
// sample-response.js class sampleresponse { constructor(message) { // validate `message` somehow this.data = message } } module.exports = { sampleresponse }
then you're structuring http interface, can send whichever response you'd (for example, express):
res.send(new sampleresponse(message))
same goes errors, etc. you're not limited lack of types javascript, have enforce things differently.
if you're not using es6, can this:
module.exports = { sampleresponse: function(message) { // validation return { data: message }; // or whatever want } };
Comments
Post a Comment