Hello, I am learning to deploy node.js program on edgio, according to the official document Express | Edgio Documentation
But it keeps failing. Are there any special modifications needed?
Hello, I am learning to deploy node.js program on edgio, according to the official document Express | Edgio Documentation
But it keeps failing. Are there any special modifications needed?
Based on the Edgio documentation provided, it seems you are encountering issues when trying to deploy your Node.js application using Express on Edgio. To assist you with the deployment process, here are a few steps and considerations that you should ensure you’ve followed:
Prerequisites: Make sure you have an Edgio account, an Edgio property, Node.js installed, and the Edgio CLI installed. (Express | Edgio Documentation - Express)
Install the Edgio CLI: If you haven’t already, install the Edgio CLI globally using npm or Yarn. (Express | Edgio Documentation - Express)
Initialize your project: Run edgio init --edgioVersion latest
in the root directory of your project to add the required Edgio dependencies and files to your project. (Express | Edgio Documentation - Express)
Configure the routes: The default routes.js
file created by edgio init
sends all requests to the Express server via a fallback route. Make sure this configuration is correct for your application. (Express | Edgio Documentation - Express)
Running your app locally: Before deploying, test your app locally using edgio dev
for development mode or edgio build
and edgio run --production
for production mode. (Express | Edgio Documentation - Express)
Deploying: Deploy your app to Edgio by running edgio deploy
in your project’s root directory. Ensure that any system-defined origin configurations are correctly set up. (Express | Edgio Documentation - Express)
Overriding the default app location: If Edgio cannot find your app in the default locations, specify the path to your app in edgio.config.js
. (Express | Edgio Documentation - Express)
Serving Static Assets: If your Express app serves static assets, you need to configure routes in your Edgio router to serve them from the edge. (Express | Edgio Documentation - Express)
Adding Additional Files Needed during SSR: If your app requires certain files at runtime, include them in the cloud bundle by modifying the serverless
section in edgio.config.js
. (Express | Edgio Documentation - Express)
Transpiling and TypeScript support: Edgio automatically transpiles JavaScript and TypeScript, but you can control this process if needed. (Express | Edgio Documentation - Express)
Bundling Options: By default, Edgio uses ESBuild, but you can try bundling with @vercel/ncc
for potentially smaller bundles. (Express | Edgio Documentation - Express)
If you have followed all these steps and are still facing issues, you may want to check the error logs for specific error messages that can guide you to the problem. Additionally, ensure that your application does not exceed the limits for cloud bundles and that all necessary environment variables are set.
For further assistance, you can reach out to Edgio support or consult the Edgio forum.
References:
Edgio Answers may provide inaccurate information and should be verified. Consult our official documentation for more information.
That guide is intended for apps built with an Express server. Looking at your deployment, your app may be better suited for our generic Node.js connector.
In edgio.config.js
, use the following:
module.exports = {
connector: '@edgio/nodejs-connector',
nodejsConnector: {
// buildFolder: '.', // Your project does not contain a build output, setting this to the current directory will cause subdirectory copy error as seen above. Normally this will be ./dist, etc
entryFile: 'index.js',
envPort: 'PORT',
devCommand: 'node index.js', // instructs Edgio how to start your app server when running `edgio dev`
},
// Use the `origins` configuration for your upstream endpoints (https://docs.edg.io/applications/v7/performance/cdn_as_code/edgio_config#origins)
// backends: {
// The origin should not point to your local server
// origin: {
// domainOrIp: 'localhost',
// hostHeader: 'localhost',
// },
// },
};
Configure routes.js
to use the Node.js connector to handle requests to your app:
const { Router } = require('@edgio/core/router');
const { connectorRoutes } = require('@edgio/connectors');
module.exports = new Router()
// This will send all requests to your Node.js app
.use(connectorRoutes);
Finally, modify your server file (index.js
) to make sure that the server starts automatically when Edgio invokes this script.
Current, you are just exporting the function so the server isn’t being started:
module.exports = () => {
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`Server is running on port:${port}`);
// your logic
return server;
};
Instead, you can immediately execute the function so when Edgio invokes your script the server starts:
module.exports = (() => {
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`Server is running on port:${port}`);
// your logic
return server;
})();
Now when you run edgio dev
, Edgio will start its dev server on localhost:3000
and the connector configuration under the hood essentially runs PORT=3001 node index.js
. Any requests to http://localhost:3000/
will be handled by .use(connectorRoutes)
and forwarded to localhost:3001
and handled by your server.
Looks like I missed some closing brackets in the code above which is why you received the truncation error. Try this:
module.exports = (() => {
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`Server is running on port:${port}`);
// your logic
return server;
})})();
Building your app for deployment on Edgio
Warning Service worker source file not found: sw/service-worker.js, skipping…
Bundling router… done.
Bundling edge functions… done.
Writing static asset aliases… done.
done 278ms
2024-10-12T03:51:58Z - info - > Uploading static assets
2024-10-12T03:51:58Z - info - > Deploying Serverless functions
2024-10-12T03:51:58Z - info - > Deploying Cloud Functions and static assets
2024-10-12T03:52:07Z - info - > Cloud deployments live at:
2024-10-12T03:52:07Z - info - → https://ohyoxo-discord-bot-main-1.free.edgio-perma.link
2024-10-12T03:52:08Z - error - Error: Failed to invoke serverless function with error: {
“statusCode”: 534,
“errorMessage”: {
“title”: “Edgio Project Error”,
“message”: “The project’s cloud function has failed unexpectedly because of uncaught exception somewhere in the user’s code or has issued a malformed response. If you are the website administrator, please use <a href="Server Logs | Edgio Documentation”>server logs to debug.“,
“statusCode”: 534,
“statusMessage”: “Edgio Project Error”,
“level”: 2,
“details”: “Cannot find module ‘express’\nRequire stack:\n- /var/task/app/index.js”,
“stack”: [
“Error: Cannot find module ‘express’”,
“Require stack:”,
“- /var/task/app/index.js”,
" at Module._resolveFilename (node:internal/modules/cjs/loader:1140:15)”,
" at Module._load (node:internal/modules/cjs/loader:981:27)“,
" at Module.require (node:internal/modules/cjs/loader:1231:19)”,
" at require (node:internal/modules/helpers:177:18)“,
" at Object. (/var/task/app/index.js:1:17)”,
" at Module._compile (node:internal/modules/cjs/loader:1364:14)“,
" at Module._extensions…js (node:internal/modules/cjs/loader:1422:10)”,
" at Module.load (node:internal/modules/cjs/loader:1203:32)“,
" at Module._load (node:internal/modules/cjs/loader:1019:12)”,
" at ModuleWrap. (node:internal/modules/esm/translators:203:29)"
]
}
}
2024-10-12T03:52:08Z - info - Build deployment failed.