diff --git a/aws-node-express-api/index.js b/aws-node-express-api/index.js index 87447791a..ca6f31d9e 100644 --- a/aws-node-express-api/index.js +++ b/aws-node-express-api/index.js @@ -1,23 +1,26 @@ -const serverless = require("serverless-http"); -const express = require("express"); -const app = express(); +const router = utils.Router(); -app.get("/", (req, res, next) => { - return res.status(200).json({ - message: "Hello from root!", - }); +router.get("/", (req, res, next) => { + return { + status: 200, + body: "Hello from root!", + }; }); -app.get("/path", (req, res, next) => { - return res.status(200).json({ - message: "Hello from path!", - }); +router.get("/path", (req, res, next) => { + return { + status: 200, + body: "Hello from path!", + }; }); -app.use((req, res, next) => { - return res.status(404).json({ - error: "Not Found", - }); +router.all("*", (req, res, next) => { + return { + status: 404, + body: "Not found", + }; }); -module.exports.handler = serverless(app); +export async function handleRequest(request) { + return router.handleRequest(request); +} diff --git a/aws-node-express-api/index.webpack.config.js b/aws-node-express-api/index.webpack.config.js new file mode 100644 index 000000000..f40c909f3 --- /dev/null +++ b/aws-node-express-api/index.webpack.config.js @@ -0,0 +1,26 @@ +const path = require("path"); + +module.exports = { + entry: "./index.js", + module: { + rules: [ + { + test: /.tsx?$/, + use: "ts-loader", + exclude: /node_modules/, + }, + ], + }, + resolve: { + extensions: [".tsx", ".ts", ".js"], + }, + output: { + path: path.resolve(__dirname, "dist"), + filename: "index.spin.js", + library: "spin", + }, + optimization: { + minimize: false, + }, +}; + diff --git a/aws-node-express-api/spin.toml b/aws-node-express-api/spin.toml new file mode 100644 index 000000000..1f6c33aaa --- /dev/null +++ b/aws-node-express-api/spin.toml @@ -0,0 +1,17 @@ +spin_manifest_version = "1" +description = "Take it for a Spin" +name = "aws-node-express-api" +trigger = { type = "http", base = "/" } +version = "0.1.0" + + +[[component]] +id = "spin-component-index" +source = "./target/index.spin.wasm" +exclude_files = ["**/node_modules"] +allowed_http_hosts = ["insecure:allow-all"] +[component.trigger] +route = "/..." +[component.build] +command = "npx --yes webpack --config=index.webpack.config.js --mode=production && mkdir -p target && ./spin js2wasm -o target/index.spin.wasm ./dist/index.spin.js" +workdir = ""