Skip to main content

Express Js Deployment

Your Expressjs project directory should look something like this:

projectRoot
|___ src
|___ <FUNCTION NAME>
|___ index.js
|___ .lyrid-definition.yml **

Where FUNCTION NAME should export your express app. This will be used as follows:

const entry = require('./src/<FUNCTION NAME>')

// ......

const port = process.env.PORT || 3000;
entry.listen(port, () => {
console.log('Listening on port', port);
});

Lyrid Definition

Your lyrid definition should look something like this:

// Your .lyrid-definition.yml
name: <YOUR APP NAME>
// ......
modules:
- name: <YOUR MODULE NAME>
language: nodejs(12|14|16|18).x
web: express
// ......
functions:
- name: <FUNCTION NAME>
// ......
note

Starting from Node 16+ most our build script uses yarn as package manager as opposed to npm. It may cause issues during package installation in Lyrid's Platform if you are not using the same package manager.

  • Node 12 - npm
  • Node 14 - npm
  • Node 16 - yarn
  • Node 18 - yarn

Example

Here is an example:

Project file structure

projectRoot
|___ src
|___ entry.js
|___ index.js
|___ .lyrid-definition.yml **

Entry.js

const express = require('express'); 
const path = require('path');
const app = express();

// Middlewares
var morgan = require('morgan')
app.use(morgan('combined'))

app.get('/env', function(req, res) {
res.json(process.env)
})

app.get('/env/:key', function(req, res) {
const envkey = req.params.key
res.json({
[envkey]: process.env[envkey]
})
})

// Default Routes - Static file + index.html handler
app.use(express.static(path.join(path.resolve(), 'dist')));
app.get('/', function(req, res) {
res.sendFile(path.join(process.cwd(), 'dist/index.html'));
});

app.get('/api/version', function(req, res) {
res.json({
'NodeJS Version': process.version,
'Cloud Environment': v1.0.0-alpha
})
});

// Error handler
app.use((err, req, res, next) => {
res.status(500).send('Internal Serverless Error: ' + err);
});

module.exports = app;

Lyrid Definition

name: nodejs16_sample_1
description: App Description
ignoreFiles: .git node_modules
modules:
- name: template
language: nodejs16.x
web: express
description: Module description
functions:
- name: entry
description: the entry point for the function