In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Express APP
As a Node.js developer, I believe everyone may use the Express framework. Whether it is building a back-end service or building a front-end development server, Express is a very popular choice. Building Express is extremely easy, add some routing rules and corresponding processing functions, and then select some middleware, and an application will be born.
A simple Express.js App that uses traditional hosting methods-- the process of responding to a single request.
The following code shows the simplest Express App:
'use strict'
Const express = require ('express')
Const app = express ()
App.get ('/', (req, res) = > res.send ('Hello worldview'))
Module.exports = app
This completes an Express App. If you use a browser to access http://localhost:3000, you can see "Hello world!" in the open web page. information.
Application deployment
The troublesome question arises: how can you show the Express App you build to your friends or family? How can everyone access it?
Application deployment is a time-consuming and painful process, but for now we assume that you have done the deployment quickly and well. Your app has been accessible to everyone and has worked well since. Until one day, there is a sudden influx of users to start using your app. Your server is starting to get tired, but it's still working.
A simple Express.js App that uses traditional hosting methods-- under a heavy load.
After going on like this for a while, it finally went down.
A simple Express.js App that uses traditional hosting methods-- applications hang up due to too many user visits
A large number of users are unhappy because the app is inaccessible (whether they pay for it or not). You despair about it and start looking for a solution on Google. Can the status quo be improved if deployed on the cloud?
Deploying on the cloud should solve the problem of application scaling, right?
At this point you meet an annoying friend who is talking to you about Serverless (serverless) technology.
Try serverless
Make your Express App Serverless.
In the past article "5-minute serverless practice | Building a serverless image porn detection Web application", you already know that Serverless API is made up of API Gateway and FunctionGraph. What you need to think about now is how to make your Express App Serveless. Just like the scene depicted in Matt Damon's movie "Shorten Life", Serverless also has unlimited potential and possibilities in the future.
How to seamlessly connect your Express App to FunctionGraph
Let's ask it for advice! Before integrating into FunctionGraph, your code needs to be adjusted a little bit. You need to export your app instead of calling app.listen to start it. Your app.js content should be similar to the following code:
'use strict'
Const express = require ('express')
Const app = express ()
App.get ('/', (req, res) = > res.send ('Hello worldview'))
Module.exports = app
After this modification, you may not be able to start the Express server locally, but you can solve it by adding additional app.local.js files:
'use strict'
Const app = require ('. / app')
Const port = process.env.PORT | | 3000
App.listen (port, () = >
Console.log (`Server is listening on port ${port} .`)
)
After that, just start the local server and execute the following command:
Node app.local.js
In order for the API of your application to be better managed using the API gateway, you also need to make sure that all your API have a common root_path. Now, go to the FunctionGraph page to create a function named root_path named api, and upload the local Express project as the code of the function. Then create an entry file for the function. Click File-> New File Template-> Node.js Express Server in the online editor to quickly create it. The entry file code is as follows:
Const fgsExpress= require ('fgs-express')
Const app = require ('. / app'); / / Your Express app entry
Const server = fgsExpress.createServer (app)
Exports.handler = (event, context, callback) = > {
FgsExpress.proxy (server, event, context, callback)
}
Fgs-express tripartite will wrap your app and forward requests between apig and app. At this point, your Serverlessized Express APP is online, open the link returned in the response message in the browser, and if the web page shows "Hello world!" Then prove that the application has been successfully deployed!
Serverless Express App
advantage
After Serverless your application, you are no longer afraid of the further expansion of the user base, and the application will remain available all the time. This is not an exaggeration, because by default FunctionGraph can support up to 100 function concurrent execution through auto scaling. When the API Gateway receives the request, the new function will be available for a short time.
Serverless Express.js App under high load
This is not the only benefit you get when you connect to Serverless. Under the premise of ensuring that the application will not go down due to high load, you have also reduced the running cost of a lot of applications. With FunctionGraph, you only have to pay for the actual number of visits to your app. Similarly, FunctionGraph's free trial program will give you 1 million free traffic per app per month (based on the number of visits).
Your Serverless App really saves you money.
Cooler Demo
Is it really possible to integrate quickly in a real project? Let's actually do a wave, find an actual Express project on Github, and make it Serverless and go live quickly.
Canfoo / react-taopiaopiao
This is an imitating ticket APP based on Express and React, which includes the processing of front-end and back-end requests without paying attention to details. We set the root_path of its Api to express-demo, then compress the project into a zip package and create a function named express-demo as code. When the creation is complete, add an entry file to the function and create an apig trigger, which completes the build. The url displayed in the Apig trigger is the root path of the Api access address of the Express program.
Now, let's wait and see, generate a QR code for this url, pull out your phone, and let people visit your APP from all over the world.
Love needs running-in-defect
Even if Servlerss Express APP sounds great, there will be some flaws.
Here are some of Serverless Express App's deadliest weaknesses:
1. Websockets cannot be used in FunctionGraph. This is because in Functiongraph, if the application does not have any access, then your server objectively does not exist.
2. Uploading files to the file system also doesn't work unless your upload directory is the / tmp folder. This is because FunctionGraph is read-only to the file system, and even if you upload files to the / tmp folder, they will only exist when function is in "working state". To ensure that the upload function in your application works properly, you should upload the file and save it to OBS.
3. Execution restrictions will also affect your Serverless Express App function. For example, the maximum execution time of FunctionGraph cannot exceed 5 minutes.
This is just a prelude to the Serverless love story between your app and FunctionGraph. Look forward to the emergence of more love stories as soon as possible!
Life needs love-thank you
The introduction to Express of this article draws lessons from the following articles:
(original text) Express.js and AWS Lambda - a serverless love story (author: Slobodan Stojanovi stories)
Express.js and AWS Lambda-- A romantic love story about Serverless.
Scan the code and experience the function workflow FunctionGraph~ for free
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.