Nodejs for api calls
Currently I have a php script that external sites access with a api key. Based on the api key it returns data back in json. I been reading up on nodejs and feel this might be a good use of nodejs since I have noticed a high load/access of the api, though Im still new at this so might be wrong if I am wrong let me know. My question is in my php script I do a lot of checks to determine what information to pass back, using nodejs should I be doing all t开发者_开发百科he checks using javascript or can I still use php with nodejs to extract the information needed to pass back as json?
EDIT
the PHP script/API consists of mysql access if that helps at all
Well it sounds like nodejs would not be the route to go in this case. I will instead go the route of either file based caching or memcache along with continue research to improve the code and db indexes/queries.
As an alternative to memcache you can cache inside the Node.js process.
app.get('/:apitype/:action/:apikey', function( req, res ) {
if( checkApiKey(req.params.apikey) === false ) {
return res.send('Invalid API key');
}
if( api[req.params.apitype][req.params.action].cache ) {
return res.send( api[req.params.apitype][req.params.action].cache );
}
query( req.params.apitype, req.params.action, function ( result ) {
api[req.params.apitype][req.params.action].cache = JSON.stringify(result);
res.send( api[req.params.apitype][req.params.action].cache );
setTimeout( function () {
api[req.params.apitype][req.params.action].cache = null;
}, 5 * 60 * 1000);
});
});
I am commenting on the first answer, but it is actually possible to run Node.js on a shared host as long as you have shell access or can compile and upload a compatible build of node. This method uses a library developed by samcday on github (http://github.com/samcday/node-fastcgi-application) and allows Node to respond to FastCGI calls. You do not need to be able to bind to a port on a public IP address and this will work with a virtual host.
精彩评论