Introduction to Serverless with Node.js: A Simple Example for Beginners

By Guilherme Luiz Maia Pinto
Picture of the author
Published on
Serverless with Node.js Banner

What is Serverless?

The serverless architecture is changing how we build and scale applications. “Serverless” does not mean “no servers”; it means the servers are abstracted away by the cloud provider. You focus on application logic while the provider handles provisioning, scaling, and maintenance.

Serverless is ideal for variable or bursty workloads such as REST APIs, event processing, and website backends.


Why Use Serverless with Node.js?

  • Speed and efficiency: The V8 engine delivers strong performance
  • Huge ecosystem: NPM offers thousands of packages
  • Asynchronous model: Great for concurrent I/O‑bound workloads

Prerequisites

Before you start:

  1. AWS account (free tier is fine)
  2. Node.js installed
  3. AWS CLI configured (aws configure)
  4. Serverless Framework (recommended)
npm install -g serverless

Create the Project

Initialize a new Serverless service for AWS and Node.js:

serverless create --template aws-nodejs --path my-function
cd my-function

This scaffolds a project with a serverless.yml that controls your function configuration.


Step 1: Write the Function Code (handler.js)

Replace the default content with:

'use strict'

module.exports.hello = async (event) => {
  return {
    statusCode: 200,
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ message: 'Hello, Serverless World!', input: event }, null, 2),
  }
}

Step 2: Configure the Service (serverless.yml)

Update serverless.yml to expose an HTTP endpoint:

service: my-function

frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs18.x
  region: us-east-1
  stage: dev

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          path: /hello
          method: get

This config deploys a Lambda function reachable at /hello via API Gateway (HTTP API).


Step 3: Deploy

serverless deploy

After deployment, the CLI prints an invoke URL. Test it with your browser or cURL:

curl https://<your-endpoint>/hello

Expected response:

{
  "message": "Hello, Serverless World!",
  "input": {}
}

Advantages of Serverless

  1. Cost‑effectiveness: pay only for execution time and requests
  2. Automatic scalability: scales up/down with traffic
  3. Reduced maintenance: focus on code, not servers

Next Steps

  • Add environment variables and secrets (AWS SSM/Secrets Manager)
  • Integrate a database (DynamoDB, RDS, or external services)
  • Add auth (Cognito, custom JWT) and observability (CloudWatch, X‑Ray)

Conclusion

Creating a serverless API with Node.js is fast and effective. You learned the basics—function code, configuration, and deployment with the Serverless Framework. From here, expand with data storage, authentication, and CI/CD to build production‑ready services.

Stay Tuned

Want to become a Software Engineer pro?
The best articles and links related to web development are delivered once a week to your inbox.