Creating a Serverless Node.js Application
To make our code executable in GCF (Google Cloud Functions), we should wrap the code inside one single function. GCF will call that particular function whenever the trigger occurs. The possible ways to do this are uploading,
- Single file: Export a default function that will call other functions based on the request.
- Multiple files: Have an
index.js
file requiring all other files and exporting the default function as starting point. - Multiple files: Have one main file configured in
package.json
using"main": "main.js"
as the starting point.
Any of the above methods will work.
GCF has a particular Node runtime version supported. Make sure the code is written to support that particular version. At the time of creating this post, GCF supports Node version v6.11.1.
To create a function, There are few options to consider.
- Memory This tells how much memory is needed to process the request for one run time. Defined in MB. For a small application, 128MB should be quite sufficient, but can increased up to 2GB.
- Timeout Timeout, as the name implies, defines the expected code execution timeout. After this, the code will be killed and stopped. Any execution after this point will stop abruptly. Max timeout is 540 seconds.
- Function to execute Though more than one function can be exported from the main handler file, we need to configure one function that should be triggered for processing the request. This allows the us to have multiple entry points based on HTTP method/URL.
Any NPM module dependency should be mentioned in package.json
. GCF attempts to install the modules mentioned in the package.json
file during the first-time setup.
Let’s create a simple handler to return a 200 status and some message. Create a function and add the following code to the source.
exports.httpServer = function httpServer(req, res) {
console.log(req);
res.status(200).send('Server is working');
}
Once the function is created, just test the function by enter the URL which is specified to trigger the function in Google Cloud Platform. After that, update the code to handle simple routes for /users
.
The following code is used to handle a simple GET
& POST
request for the /users
route:
exports.httpServer = function httpServer(req, res) {
const path = req.path;
switch(path) {
case '/users':
handleUsers(req, res);
break;
default:
res.status(200).send('Server is working');
}
};
const handleUsers = (req, res) => {
if (req.method === 'GET') {
res.status(200).send('Listing users...');
} else if (req.method === 'POST') {
res.status(201).send('Creating User...')
} else {
res.status(404);
}
}
After updating, test it in-browser with /users
at the end. Finally, we created a basic HTTP server with routing.
Hire Node.js Developer from us, as we give you high quality product by utilizing all the latest tools and advanced technology. E-mail us any clock at – hello@hkinfosoft.com or Skype us: “hkinfosoft“.
To develop the custom web app using Node.js, please visit our technology page.
Content Source:
- toptal.com