Today’s post is a little short and a little late as I’m pretty busy at work!
I’m going to do a quick functional programming example that I do in my routing of my application, now there probably is an easier way, but as allways programming is a process.
Here’s the code:
_boolHandle = (_f) -> (req, res) -> _testExists req.params.ticketID, res, -></pre> _f req, res, (err, results) -> if err then res.status(500).json err else res.status(200).json results or true app.post '/api/ticket/:ticketID/metadata', _boolHandle (req, res, cb) -> $tickets.metadata req.params.ticketID, req.body, cb
This is essentially a post handler in express 4 that calls a function that returns a function to call a function to call a standard response!
If we break it down, when we register the handler, normally this would be written as follows:
app.post '/api/ticket/:ticketID/metadata', (req, res) -> res.status(200).json true
My first step was figuring out that every call I made to the $ticket object calls it’s callback with (err, result) and returned the results, so my code then looks like this:
app.post '/api/ticket/:ticketID/metadata', (req, res) -> $ticket.metadata req.params.ticketID, (err, result) -> if err then res.status(500).json err else res.status(200).json result
Because I’m lazy I decided I didn’t want to write out the callback every time, it’s always the same so why bother? So my first functional programming block was this:
_boolHandle = (_f) -> (req, res) -> _f req, res, (err, results) -> if err then res.status(500).json err else res.status(200).json results or true app.post '/api/ticket/:ticketID/metadata', _boolHandle (req, res, cb) -> $tickets.metadata req.params.ticketID, cb
As you can see I abstracted out the response by creating a wrapper around it. The other part I have in the code is the function that tests whether the ticketID actually exists (_testExists), if it exists it calls the callback else it returns a “ticket not found” error.
I hope this explanation helps someone out, if you have a better way of doing this – I’m all ears!!
This week I am going to outline the difficulties I have in getting my app running as a service on windows.
Normally I would start up my server either by calling “coffee app.coffee” or “npm start”. If you look around at the blogs and posts about this you’ll find that all of them refer to running the service via “node app.js”. Which just plain won’t work for me.
Beforehand I’ve used nssm, so I decided to give it a go again, I settled for getting it to run “npm start”.
- Figure out the location of your node.js installation (node.exe), for me it was “C:\Program Files\nodejs\node.exe”.
- Fire up nssm in the add service mode.
- Enter in your node installation for Application Path.
- Enter in the directory of your project in Startup directory.
- In Arguments put in the quoted location of the npm-cli.js file and then “start” (with quotes). This should be relative to your node installation, for me it’s “C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js”.
In total the Arguments field should look like:
"C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "start"
- Hit save and your service should run as if you called “npm start”!