How to Dodge Common Pitfalls with Midway FaaS Serverless Development
This guide walks front‑end developers through the essential steps, tools, and best practices for using Midway FaaS to build serverless applications, highlighting four key pitfalls and how to avoid them while simplifying deployment across major cloud providers.
Every era offers opportunities, and the cloud‑native wave has been rolling since 2014. In 2020, major cloud vendors are ready, and front‑end developers must decide whether to embrace the sweet (or thorny) benefits of serverless.
Alibaba has spent a year integrating Serverless with existing frameworks to simplify cloud‑function development for the community.
This article explains how to avoid common pitfalls when adopting Midway FaaS.
Avoid Pitfall #1
Midway’s Web stack attempts to transition from traditional Web to Serverless, but clean code can become restrictive. Choosing a cloud platform is the first trap; instead of being swayed by ads, Midway FaaS uses a fixed template and requires its CLI tool. npm i @midwayjs/faas-cli -g The CLI, written in Node.js, is often the first touchpoint for front‑end developers entering Serverless and integrates services from Alibaba Cloud, Tencent Cloud, and others, supporting development, debugging, and deployment.
Creating a function example: f create Midway uses class‑based code with decorators, allowing reusable functions and traditional OOP capabilities.
// Midway IoC special marker, expose capabilities
@Provide()
export class MyFirstFunctionClass {
// First function
@Func('api.user')
async myFn1() {}
// Second function
@Func('api.book')
async myFn1() {}
// Third function
@Func('api.store')
async myFn1() {}
}Avoid Pitfall #2
Many cloud platforms showcase simple handler functions, but they differ subtly in response handling.
exports.handler = (event, context, callback) => {
callback(null, 'hello world');
}
exports.handler = (request, response, context) => {
response.send('hello world');
}Front‑end developers familiar with Koa/Egg will find Midway FaaS code almost identical, allowing a quick start using familiar APIs.
// Midway IoC special marker, expose capabilities
@Provide()
export class MyFirstFunctionClass {
@Inject()
ctx;
@Func('api.user')
async myFn1() {
this.ctx.body = 'hello world';
}
@Func('api.book')
async myFn1() {
this.ctx.type = 'html';
this.ctx.body = '<html><body>hello world</body></html>';
}
@Func('api.store')
async myFn1() {
const data = await request('http://xxxx/api/data.json');
this.ctx.set('X-HEADER-TIMEOUT', 2000);
this.ctx.body = { data: { success: true, result: data } };
}
}Avoid Pitfall #3
Testing Serverless functions traditionally requires Docker or direct deployment, which can be cumbersome for front‑end developers. Using the invoke command enables local invocation and service startup without Docker. f invoke -p The familiar http://127.1:3000 endpoint is launched, allowing seamless development and debugging.
Avoid Pitfall #4
After development and testing, deployment to Alibaba Cloud or Tencent Cloud is a matter of configuration.
# f.yml
provider:
name: aliyun // tencentThen deploy: f deploy Serverless reduces operational overhead and cost, but introduces a gap from traditional development. Midway FaaS aggregates resources via YAML, allowing business logic to focus on single functions while the platform handles scaling and resource provisioning.
However, managing many interfaces still raises questions about publishing and management.
Midway FaaS supports interface aggregation, letting you configure in f.yml whether to bundle all interfaces into one function container or separate hot paths, without code changes.
# f.yml example for custom deployment
provider:
name: aliyun
# additional configuration hereImages illustrating configuration and CLI usage:
Final Checklist
Avoid early platform selection; focus on development.
Reuse existing capabilities instead of reinventing wheels.
Prefer local debugging over platform‑only testing.
Minimize extra costs by using free tiers.
By following these guidelines, you can build simple web interfaces with minimal cost and avoid common serverless traps.
Midway FaaS is a front‑end‑friendly entry to Serverless, offering dependency injection, decorators, configuration management, componentization, custom runtimes, and private deployment options.
Midway will always be with you .
Repository: https://github.com/midwayjs/midway-faas
Documentation: https://www.yuque.com/midwayjs/faas
Alibaba Cloud Workbench now provides ready‑made Midway FaaS examples for web stack and SSR, deployable with a click, supporting traditional development, extensibility, and function flexibility without cost.
https://developer.aliyun.com/topic/workbench
Node Underground
No language is immortal—Node.js isn’t either—but thoughtful reflection is priceless. This underground community for Node.js enthusiasts was started by Taobao’s Front‑End Team (FED) to share our original insights and viewpoints from working with Node.js. Follow us. BTW, we’re hiring.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
