Custom made lightweight template engine for Node.js
https://codesandbox.io/s/nodejs-template-engine-md1hb
- Light weight
- Has it's own parsing template literal:
$nv{}
- We can pass objects through the routes like so:
if (req.url === '/') {
httpStaticCall.serve(req, res, {
uri: req.url,
method: req.method,
host: req.headers.host,
remoteAddress: req.connection.remoteAddress
});
}
- Then we can use and access those objects by using our template literal
$nv{}
like so:
<div>
<h3>Dynamic Object Variables Processed By Template Engine:</h3>
<span>
<span>Request URI: $nv{uri}</span><br/>
<span>Request Method: $nv{method}</span><br/>
<span>Request Host: $nv{host}</span><br/>
<span>Request Remote Host: $nv{remoteAddress}</span><br/><br/>
</span>
</div>
npm install
npm start
- Open http://localhost on any browser of your choice.
- You will see an web page which contains Request URI, Request Method, Request Host, Request Remote Host variables
- Those variables are rendered by our template engine which parses by
$nv{}
template literal - Click on the "Form post to mongodb test" link or open http://localhost/get-form-test.html
- You will see a form with
Msg id
andMsg
field
- Put any random id and random message into the form and click on "Submit" button
- It will post the form data through the api
- You will get back json response from the api on successful insertion of documet to mongodb collection
- The JSON response uses template our custon engine to render it to browser
JSON Response like this:
[{"_id":"61c1d8b039e9a2004a1771b6","test-msg-id":"d456","test-msg":"User data"}]