Skip to content

Latest commit

 

History

History
105 lines (75 loc) · 2.22 KB

node-based.md

File metadata and controls

105 lines (75 loc) · 2.22 KB
title description
Node-based Web Frameworks
Build apps on Ampt using your favorite web frameworks with zero boilerplate.

The http interface from the @ampt/sdk provides a node interface method that lets you integrate your favorite Node-based web frameworks into an Ampt app. The http.node.use method wraps the instance of your framework and exposes any defined routes on the root of your public *.ampt.app URL.

Ampt runs your web frameworks automatically, so you DO NOT need to use .listen or .createServer.

Below are some examples for the most popular web frameworks.

!!! note The default request timeout for web frameworks is 29 seconds. !!!

Express.js

import { http } from "@ampt/sdk";

import express from "express";
const expressApp = express();

expressApp.use("/express", (req, res) => {
  res.send("hello express");
});

http.node.use("/api", expressApp);

Connect

import { http } from "@ampt/sdk";
import connect from "connect";
const connectApp = connect();

connectApp.use("/connect", (req, res) => {
  res.end("hello connect");
});

http.node.use(connectApp);

Koa

import { http } from "@ampt/sdk";
import Koa from "koa";
import KoaRouter from "@koa/router";

const koaRouter = new KoaRouter();
const koaApp = new Koa();
koaRouter.get("/koa", (ctx, _next) => {
  ctx.status = 200;
  ctx.body = "hello koa";
});

koaApp.use(koaRouter.routes()).use(koaRouter.allowedMethods());

http.node.use("/api", koaApp);

Restana

import { http } from "@ampt/sdk";
import restana from "restana";
const restanaApp = restana();

restanaApp.get("/restana", (req, res) => {
  res.send("hello restana");
});

http.node.use("/api", restanaApp);

Fastify

import { http } from "@ampt/sdk";
import fastify from "fastify";
const fastifyApp = fastify();

fastifyApp.get("/fastify", (req, res) => {
  res.send("hello fastify");
});

http.node.use("/api", fastifyApp);

Restify

import { http } from "@ampt/sdk";
import restify from "restify";
const restifyApp = restify.createServer();

restifyApp.get("/restify", (req, res, next) => {
  res.send("hello restify");
  next();
});

http.node.use("/api", restifyApp);