Skip to content

Commit

Permalink
add node-express example
Browse files Browse the repository at this point in the history
  • Loading branch information
cnuss committed Oct 6, 2024
1 parent 2001a58 commit 86bef89
Show file tree
Hide file tree
Showing 10 changed files with 858 additions and 14 deletions.
61 changes: 61 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next
50 changes: 36 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# A CHANGEME-FRAMEWORK App Running On AWS Lambda
# An ExpressJS App Running On AWS Lambda

![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/scaffoldly/scaffoldly-examples/scaffoldly.yml?branch=CHANGEME-BRANCHNAME&link=https%3A%2F%2Fgithub.com%2Fscaffoldly%2Fscaffoldly-examples%2Factions)
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/scaffoldly/scaffoldly-examples/scaffoldly.yml?branch=node-express&link=https%3A%2F%2Fgithub.com%2Fscaffoldly%2Fscaffoldly-examples%2Factions)

This application was generated with the following command:

```bash
CHANGEME-CREATECOMMAND
npx express-generator --no-view --git
```

✨ No modifications or SDKs were made or added to the code to "make it work" in AWS Lambda.
Expand All @@ -14,17 +14,39 @@ Check out our other [examples](https://github.com/scaffoldly/scaffoldly-examples

### Working example

[CHANGEME-URL](CHANGEME-URL)
- `index.html` Page: [https://pbydasw2o3quxi7fu3dyiqomne0noypf.lambda-url.us-east-1.on.aws](https://pbydasw2o3quxi7fu3dyiqomne0noypf.lambda-url.us-east-1.on.aws)
- `/users` API: [https://pbydasw2o3quxi7fu3dyiqomne0noypf.lambda-url.us-east-1.on.aws/users](https://pbydasw2o3quxi7fu3dyiqomne0noypf.lambda-url.us-east-1.on.aws/users)

## First, Scaffoldly Config was added...

In the project's [`CHANGEME-CONFIGFILE`](CHANGEME-CONFIGFILE) file, the `scaffoldly` configuration was added:

- Note 1
- Note 2

```
CHANGEME-CONFIG
In the project's [`package.json`](./package.json) file, the `scaffoldly` configuration was added:

- `files` includes all directories needed at runtime
- the `start` command piggybacks off of the `npm start` command

```jsonc
"scaffoldly": {
"runtime": "node:22-alpine",
"handler": "localhost:3000",
"services": [
{
"name": "express",
"files": [
"bin",
"node_modules",
"public",
"routes",
"app.js",
"package.json",
"package-lock.json"
],
"scripts": {
"install": "npm ci",
"start": "DEBUG=test:* npm start"
}
}
]
}
```

See the [Scaffoldly Docs](https://scaffoldly.dev/docs/config/) for additional configuration directives.
Expand All @@ -41,10 +63,10 @@ See the [Scaffoldly Docs](https://scaffoldly.dev/docs/cli/#scaffoldly-deploy) fo

```bash
🚀 Deployment Complete!
🆔 App Identity: CHANGEME-IDENTITY
🆔 App Identity: arn:aws:iam::123456789012:role/node-express-69c935be
📄 Env Files: .env.main, .env
📦 Image Size: CHANGEME-IMAGESIZE MB
🌎 URL: CHANGEME-URL
📦 Image Size: 193.07 MB
🌎 URL: https://pbydasw2o3quxi7fu3dyiqomne0noypf.lambda-url.us-east-1.on.aws
```

## GitHub Action added for CI/CD
Expand Down
20 changes: 20 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

module.exports = app;
86 changes: 86 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require("../app");
var debug = require("debug")("node-express:server");
var http = require("http");

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || "3000");
app.set("port", port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on("error", onError);
server.on("listening", onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== "listen") {
throw error;
}

var bind = typeof port === "string" ? "Pipe " + port : "Port " + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
debug("Listening on " + bind);
}
Loading

0 comments on commit 86bef89

Please sign in to comment.