Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
serverless-express
Advanced tools
Make express apps compatible with serverless framework. Ensure compatibility with serverless-offline plugin.
Works with provider :
Type this command inside your terminal
npm install --save serverless-express
or for yarn users
yarn add serverless-express
inside your project's serverless.yml file add serverless-express
to the plugin list inside serverless.yml
It should look something like this:
plugins:
- serverless-foo # <- fake name
- serverless-express # <- like so
NB: In order to automatically know on which provider the function is executed,
the plugin asks serverless to set SERVERLESS_EXPRESS_PLATFORM
as an environment variable. The plugin uses it internally
In the head of your express app file :
require('express')
by require('serverless-express/express')
it should look like this:
// const express = require('express');
const express = require('serverless-express/express')
var app = express();
// Q: What is serverless-express/express ??
//
// A: It's express, the exact same express that you already use.
// We wrapped it and we abstracted away all specific implementation
// that has to be done in order to work properly on every provider.
//
// Basicaly, we just made express development provider agnostic
// so you can develop your app without any vendor lock-in, yeah !
// --- example code ---
//
// app.get('/some_url', doSomething() )
// app.post('/other_url', doSomethingElse() )
//
// ------- end --------
module.exports = app
// app.listen(PORT, ()=>{
// console.log('listening')
// })
inside the handler file, you have to import serverless-express/handler
and call it with your express app.
It should look something like this:
const handler = require('serverless-express/handler')
const app = require('path/to/your/express/app')
module.exports.api = handler(app)
// that's it ;)
Now that everything is done, you can get back to work and enjoy serverless and express in the same time ;)
Make sure that you register an HTTP event for each endpoint of your express app. For example, if you register an endpoint like this one:
app.get( '/users' , showUsers )
You will need to make sure that your cloud provider routes the HTTP call to your app. In order to do so, you will need to update your serverless.yml like so :
# like this
functions:
app:
handler: handler.handler #assuming your handler file is handler.js
events:
- http:
method: GET
path: /users
# or like this
# be careful, this will route every HTTP event to your function.
functions:
app:
handler: handler.handler
events:
- http:
method: ANY
path: /{proxy+}
FAQs
Run express apps on AWS λ easily.
The npm package serverless-express receives a total of 1,795 weekly downloads. As such, serverless-express popularity was classified as popular.
We found that serverless-express demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.