Serverless Express ⎯⎯⎯ This Serverless Framework Component enables you to take existing Express.js apps and deploy them onto cheap, auto-scaling, serverless infrastructure on AWS (specifically AWS HTTP API and AWS Lambda), easily. It's packed with production-ready features, like custom domains, SSL certificates, canary deployments, and costs an average of $0.000003 per request.
Check out the Serverless Fullstack Application for a ready-to-use boilerplate and overall great example of how to use this Component.
Get Started:
Extra:
To get started with this component, install the latest version of the Serverless Framework:
$ npm install -g serverless
After installation, make sure you connect your AWS account by setting a provider in the org setting page on the Serverless Dashboard.
The easiest way to start using the express component is by initializing the express-starter
template. Just run this command:
$ serverless init express-starter
$ cd express-starter
This will also run npm install
for you. You should now have a directory that looks something like this:
|- app.js
|- node_modules
|- package.json
|- serverless.yml
Once you have the directory set up, you're now ready to deploy. Just run serverless deploy
from within the directory containing the serverless.yml
file. Your first deployment might take a little while, but subsequent deployment would just take few seconds. After deployment is done, you should see your express app's URL. Visit that URL to see your new app live.
Note: If you see an internal server error
, it probably means you did not run npm install
after serverless create
. See above for more info.
For more information on what's going on during deployment, you could specify the serverless deploy --debug
flag, which would view deployment logs in realtime.
The Express component is a zero configuration component, meaning that it'll work out of the box with no configuration and sane defaults. With that said, there are still a lot of optional configuration that you can specify.
Here's a complete reference of the serverless.yml
file for the express component:
component: express # (required) name of the component. In that case, it's express. You will want to pin this to a specific version in production via semantic versioning, like this: express@1.0.10. Run 'serverless registry express' to see available versions.
name: express-api # (required) name of your express component instance.
org: serverlessinc # (optional) serverless dashboard org. default is the first org you created during signup.
app: myApp # (optional) serverless dashboard app. default is the same as the name property.
stage: dev # (optional) serverless dashboard stage. default is dev.
inputs:
src: ./ # (optional) path to the source folder. default is a hello world app.
memory: 512 # (optional) lambda memory size.
timeout: 10 # (optional) lambda timeout.
description: My Express App # (optional) lambda & api gateway description.
env: # (optional) env vars.
DEBUG: 'express:*' # this express specific env var will print express debug logs.
roleName: my-custom-role-name # (optional) custom AWS IAM Role name for setting custom permissions.
traffic: 0.2 # (optional) traffic percentage to apply to this deployment.
layers: # (optional) list of lambda layer arns to attach to your lambda function.
- arn:aws:first:layer
- arn:aws:second:layer
domain: api.serverless.com # (optional) if the domain was registered via AWS Route53 on the account you are deploying to, it will automatically be set-up with your Express app's API Gateway, as well as a free AWS ACM SSL Cert.
vpc: # (optional) vpc configuration to apply on the express lambda function
securityGroupIds:
- abc
- xyz
subnetIds:
- abc
- xyz
region: us-east-2 # (optional) aws region to deploy to. default is us-east-1.
Once you've chosen your configuration, run serverless deploy
again (or simply just serverless
) to deploy your changes.
Now that you've got your basic express app up and running, it's time to develop that into a real world application. Instead of having to run serverless deploy
everytime you make changes you wanna test, run serverless dev
, which allows the CLI to watch for changes in your source directory as you develop, and deploy instantly on save.
To enable dev mode, simply run serverless dev
from within the directory containing the serverless.yml
file.
Dev mode also enables live streaming logs from your express app so that you can see the results of your code changes right away on the CLI as they happen.
Anytime you need to know more about your running express instance, you can run serverless info
to view the most critical info. This is especially helpful when you want to know the outputs of your instances so that you can reference them in another instance. You will also see a url where you'll be able to view more info about your instance on the Serverless Dashboard.
It also shows you the status of your instance, when it was last deployed, and how many times it was deployed. To dig even deeper, you can pass the --debug
flag to view the state of your component instance in case the deployment failed for any reason.
If you wanna tear down your entire express infrastructure that was created during deployment, just run serverless remove
in the directory containing the serverless.yml
file. The express component will then use all the data it needs from the built-in state storage system to delete only the relavent cloud resources that it created.
Just like deployment, you could also specify a --debug
flag for realtime logs from the express component running in the cloud.
This is the AWS serverless infrastructure that is created by this Component:
domain
input and the domain already exists on your AWS account, a Route53 hosted zone will be created and integrated into your API Gateway.domain
input and the domain already exists on your AWS account, a free AWS ACM SSL certificate will be created.The Express Component can easily set up a custom domain and free SSL certificate for you.
First, register your custom domain via Route53 on the AWS Acccount you are deploying to.
Next, add the domain to the domain
in inputs
in serverless.yml
, like this:
inputs:
src: ./
domain: serverlessexpress.com
You can also use a subdomain:
inputs:
src: ./
domain: express.component-demos.com
Run serverless deploy
.
Keep in mind, it will take AWS CloudFront and AWS Route53 and DNS up to 24 hours to propagate these changes and make your custom domain globally accessible. However, with recent AWS CloudFront speed increases, your domain should be accessible within ~20 minutes.
If your domain is not on AWS Route53, you will have to set this up manually because the component does not have access to your registrar. Here are the general steps involved:
*.yourdomain.com
as well to include all subdomains as well.PENDING_VALIDATION
status. Now you will need to validate your domain. We suggest you follow the DNS steps by adding the validation CNAME record you see on the AWS console to your domain via your registrar dashboard.ISSUED
. Usually it takes around 5 minutes.serverless.yml
file as shown above and deploy. This step is important as it adds your domain to API Gateway.name
, stage
, app
or org
properties in serverless.yml
, this would result in a completely new instance with a new cloudfront url. This allows you to setup different domains for each stage or instanceAt scale, when you want to push changes out to a small set of users, Serverless Express offers easy Canary Deployments out of the box!
This enables you to push out a version of your app (containing code changes you deem risky) which is only served to a percentage of traffic that you specificy (0-99%). This allows you to test big changes with little risk.
To perform a canary deployment, first update your code with the potentially risky change.
Next, set a traffic weighting in your serverless.yml
inputs
:
inputs:
src: ./
traffic: 0.5 # 50%
This tells Serverless Express to serve the new (potentially risky) code to 50% of the API requests, and the old (stable) code to the other 50% of requests.
Run serverless deploy
. After deployment is complete, 50% of your requests will be randomly handled by the new experimental code.
You can slowly increment the percentage over time, just continue to re-deploy it.
If things aren't working, revert your code to the old code, remove the traffic
configuration option, and deploy.
If things are working, keep the new code, remove the traffic
configuration option, and deploy.
The Express Component uses AWS HTTP API, which can offer an API with CORS enabled or not enabled based on the headers you set in your code's responses.
For CORS support you do not need to configure it within the HTTP API infrastructure. You have complete control the CORS behavior of your application via setting traditional CORS headers, like this:
/**
* Configure Express.js Middleware
*/
// Enable CORS
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', '*')
res.header('Access-Control-Allow-Headers', '*')
res.header('x-powered-by', 'serverless-express')
next()
})
If you run into a CORS issue, ensure you are setting up your Express app to return the right headers, like above.
THe biggest reason why CORS errors can happen is because users do not capture errors correctly, and then return a correct HTTP response (with the headers above) in that error response. It may look like a CORS error, but actually, your code is crashing and the automatic error response from HTTP API does not contain the CORS headers.
本文从安装、用法、常用 API 等方面简单介绍了 Express 的用法。 一、Express 简介 1、Express 是什么? 基于 Node 平台开发的 Web 开发框架 提供一系列强大的特性,帮助创建各种 Web和移动设备应用 是 NodeJS 的一个模块 2、为什么使用 Express 为了基于 NodeJS 开发 web 应用程序更高效 二、安装与使用 1、安装 # 新建 my-app
1.1-学习环境 Node.js : 0.10.32 Express : 4.10.2 MongoDB : 2.6.1 Nodemon: 2.0.19 1.2-快速开始 安装 Express express 是 Node.js 上最流行的 Web 开发框架,正如他的名字一样,使用它我们可以快速的开发一个 Web 应用。我们用 express 来搭建我们的博客,打开命令行,输入: $ npm i
问题内容: 我正在尝试从Java程序连接到在本地主机上运行的MS SQL Server 2005 Express数据库。 我已经尝试过在另一个运行MS SQL Server 2000的系统(相同的Jave代码)上使用的连接URL(如下)。但这是行不通的。 有任何想法吗? 问题答案: 您确定这是正确的实例吗?SQL Express倾向于作为命名实例(例如“ localhost \ SQLExpres
问题内容: 我的配置: Windows XP SP3 JDBC 2005 MS SQL Server 2008 Express,通过TCP / IP在端口1433上公开 类路径中的sqljdbc.jar 我试过了: 但是它总是抛出一个异常: 我还尝试了以下网址: 结果相同。有什么帮助吗? 问题答案: 您输入的网址错误。 我不知道您所说的“ JDBC 2005”是什么意思。当我在Microsoft网
问题内容: 如果一个用户已经登录并尝试在新实例中再次登录,我希望它注销另一个用户实例。我不希望同一用户在我的应用程序上登录两次。 当前会话存储在Redis存储中,我正在使用express / connect处理会话存储。可以用来破坏会话的可用功能之一如下: 但是,我需要在调用.destroy()之前找到该会话ID。在Redis中,用户名存储为会话的一部分。 问题:是否可以查询Redis以基于用户名
问题内容: 注意对于那些使用Redis挣扎的人,必须启动Redis服务器。 在Windows上,有一个redis发行版,请查看以下链接:https : //github.com/dmajkic/redis/downloads,然后通过启动“ redis-server.exe”来启动服务器。 我正在遵循有关node.js的教程。本教程使用Express和Redis。我安装了redis和connect
问题内容: 我想用一些登录用户创建一个Django应用程序。另一方面,由于我需要一些实时功能,因此我想使用Express.js应用程序。 现在,问题是,我不希望未经身份验证的用户访问Express.js应用程序的数据。因此,我必须在Express.js和Django应用程序之间共享会话存储。 我认为使用Redis是个好主意,因为volatile键非常适合这种情况,而且我已经在应用程序的另一部分使用
问题内容: 我当前正在使用一个Web应用程序,每次都调用此Web应用程序,请删除所有当前的Express会话,因此我需要一种方法来保留所有这些会话。我试图用connect- mongodb和connect- redis保留所有这些会话,但是都无效,nodemon总是说req.session是未定义的。我不知道该怎么做才能保留所有会话。 我需要一种方法来保留所有会话,执行时不要丢失它们,并为其提供教
本文向大家介绍快速搭建Node.js(Express)用户注册、登录以及授权的方法,包括了快速搭建Node.js(Express)用户注册、登录以及授权的方法的使用技巧和注意事项,需要的朋友参考一下 项目准备 建立一个文件夹,这里叫 EXPRESS-AUTH npm init -y 启动服务 新建一个server.js 或者 app.js npm i express 开启端口,启动服务 在命令行运
问题内容: 我正在尝试在我的Express应用程序中使用redis进行会话。 我执行以下操作: 稍后,在我的应用程序中,如果执行以下操作: 我得到: 无法读取未定义的属性“ someProperty” 这表明req.session是未定义的 (我可以从config部分的console.log条目中看到) 我肯定有redis运行,并且可以看到我的应用最初连接到它(使用redis-cli监视器) 问题