#expressjs mimum hello world example with typescript and serverless functions to deploy on vercel

1 messages · Page 1 of 1 (latest)

sturdy hornet
#

Step by step

bold krakenBOT
#

Hi @sturdy hornet. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Hmm, I don't know enough to give you a confident answer yet. It seems like you're looking for a step-by-step guide, but I need more context or a specific topic related to Next.js to provide you with the appropriate information. In the meantime, you can refer to the Next.js documentation (https://nextjs.org/docs) for more information on various topics.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

Welcome to the Next.js Documentation.

#

@sturdy hornet kapa.ai is still learning and improving, please let us how it did by reacting below

sturdy hornet
#

@bold kraken standalone express api in vercel without next.js

bold krakenBOT
#

Let me check the knowledge base for you. One moment! :books:

#

To create a standalone Express.js API in Vercel without Next.js, follow these steps:

  1. Create a file index.js and add it to an /api folder. This is similar to the app.js file in serverful Express.js applications. The /api directory is where you'll add your Serverless Functions.
const app = require('express')();
const { v4 } = require('uuid');

app.get('/api', (req, res) => {
  const path = `/api/item/${v4()}`;
  res.setHeader('Content-Type', 'text/html');
  res.setHeader('Cache-Control', 's-max-age=1, stale-while-revalidate');
  res.end(`Hello! Go to item: <a href="${path}">${path}</a>`);
});

app.get('/api/item/:slug', (req, res) => {
  const { slug } = req.params;
  res.end(`Item: ${slug}`);
});

module.exports = app;

(Source: Using Express.js with Vercel (https://vercel.com/guides/using-express-with-vercel))

  1. Add a vercel.json at the root of your project to specify your app's behavior.
{
  "rewrites": [{ "source": "/api/(.*)", "destination": "/api" }]
}

(Source: Using Express.js with Vercel (https://vercel.com/guides/using-express-with-vercel))

  1. To serve static content, add a /public folder to the root of your project. This approach is recommended instead of using express.static.

(Source: Using Express.js with Vercel (https://vercel.com/guides/using-express-with-vercel))

  1. Push your project to a Git repository.

  2. Import the project into Vercel using your Git provider (https://vercel.com/import/git) of choice.

#
  1. After your project has been imported, all subsequent pushes to branches will generate Preview Deployments (https://vercel.com/docs/concepts/deployments/preview-deployments#), and all changes made to the Production Branch (https://vercel.com/docs/concepts/deployments/git#production-branch) (commonly "main") will result in a Production Deployment (https://vercel.com/docs/concepts/deployments/environments#production).

(Source: Using Express.js with Vercel (https://vercel.com/guides/using-express-with-vercel))

Keep in mind that deploying an Express.js application as a single Serverless Function comes with drawbacks and should only be used as a migration path. It's recommended to use Next.js or embrace multiple Serverless Functions as you incrementally migrate to the Vercel platform.

(Source: Using Express.js with Vercel (https://vercel.com/guides/using-express-with-vercel))

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

Vercel Documentation

Preview Deployments allow you to preview changes to your app in a live deployment without merging those changes to your Git project's production branch.

Vercel Documentation

Vercel allows for automatic deployments on every branch push and merges onto the Production Branch of your GitHub, GitLab, and Bitbucket projects.

Vercel Documentation

Environments are the solution for the deployment lifecycle on the Vercel platform. Learn about the production and preview environment types here.

#

@sturdy hornet kapa.ai is still learning and improving, please let us how it did by reacting below

sturdy hornet
#

@bold kraken but use typescript

bold krakenBOT
#

Let me check the knowledge base for you. One moment! :books:

#

To create a standalone Express.js API in Vercel without Next.js using TypeScript, follow these steps:

  1. Create a file index.ts and add it to an /api folder. This is similar to the app.js file in serverful Express.js applications. The /api directory is where you'll add your Serverless Functions.
import express from 'express';
import { v4 } from 'uuid';

const app = express();

app.get('/api', (req, res) => {
  const path = `/api/item/${v4()}`;
  res.setHeader('Content-Type', 'text/html');
  res.setHeader('Cache-Control', 's-max-age=1, stale-while-revalidate');
  res.end(`Hello! Go to item: <a href="${path}">${path}</a>`);
});

app.get('/api/item/:slug', (req, res) => {
  const { slug } = req.params;
  res.end(`Item: ${slug}`);
});

export default app;
  1. Add a vercel.json at the root of your project to specify your app's behavior.
{
  "rewrites": [{ "source": "/api/(.*)", "destination": "/api" }]
}

(Source: Using Express.js with Vercel (https://vercel.com/guides/using-express-with-vercel))

  1. To serve static content, add a /public folder to the root of your project. This approach is recommended instead of using express.static.

(Source: Using Express.js with Vercel (https://vercel.com/guides/using-express-with-vercel))

  1. Push your project to a Git repository.

  2. Import the project into Vercel using your Git provider (https://vercel.com/import/git) of choice.

#
  1. After your project has been imported, all subsequent pushes to branches will generate Preview Deployments (https://vercel.com/docs/concepts/deployments/preview-deployments#), and all changes made to the Production Branch (https://vercel.com/docs/concepts/deployments/git#production-branch) (commonly "main") will result in a Production Deployment (https://vercel.com/docs/concepts/deployments/environments#production).

(Source: Using Express.js with Vercel (https://vercel.com/guides/using-express-with-vercel))

Keep in mind that deploying an Express.js application as a single Serverless Function comes with drawbacks and should only be used as a migration path. It's recommended to use Next.js or embrace multiple Serverless Functions as you incrementally migrate to the Vercel platform.

(Source: Using Express.js with Vercel (https://vercel.com/guides/using-express-with-vercel))

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

Vercel Documentation

Environments are the solution for the deployment lifecycle on the Vercel platform. Learn about the production and preview environment types here.

Vercel Documentation

Preview Deployments allow you to preview changes to your app in a live deployment without merging those changes to your Git project's production branch.

Vercel Documentation

Vercel allows for automatic deployments on every branch push and merges onto the Production Branch of your GitHub, GitLab, and Bitbucket projects.

#

@sturdy hornet kapa.ai is still learning and improving, please let us how it did by reacting below

sturdy hornet
#

@bold kraken explain drawbacks express and vercel serverless

bold krakenBOT
#

Let me check the knowledge base for you. One moment! :books:

#

There are several drawbacks when using Express.js with Vercel Serverless Functions:

  1. Templating and View Engines: When using Next.js, React is your view layer. Using a view engine like Pug or EJS with Serverless Functions can be expensive and inefficient. To prevent computing on demand and ensure cached content can be used in the future, you need to set up the correct headers (https://vercel.com/docs/concepts/edge-network/caching).

    (Source: Using Express.js with Vercel (https://vercel.com/guides/using-express-with-vercel))

  2. Websockets: Serverless Functions have maximum execution limits (https://vercel.com/docs/concepts/limits/overview#general-limits) and should respond as quickly as possible. They should not subscribe to data events. Instead, use a client that subscribes to data events and a Serverless Function that publishes new data. Consider using a serverless friendly Realtime (https://vercel.com/docs/solutions/realtime) provider.

    (Source: Using Express.js with Vercel (https://vercel.com/guides/using-express-with-vercel))

  3. Serverless Computing: Running a full server implementation on each request is an anti-pattern, as Serverless Functions should serve one purpose. The serverless approach is different, with pieces of backend code running in stateless compute environments that are triggered by events and may last for one invocation.

    (Source: Using Express.js with Vercel (https://vercel.com/guides/using-express-with-vercel))

  4. Cold Starts: Serverless Functions can take hundreds of milliseconds to boot up before they begin processing requests. Depending on the amount of traffic your site receives, this could be a frequent occurrence as the functions are not frequently "warm".

Vercel Documentation

Vercel's Edge Network sits in-between the internet and your Vercel deployments and acts as a CDN with the ability to execute functions at the edge.

Vercel Documentation

Learn how to integrate realtime communication to your Vercel application using providers like Ably or Supabase.

Vercel Documentation

This reference covers a list of all the limits and limitations that apply on Vercel.

#

(Source: Next.js Documentation (https://nextjs.org/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes))

  1. Databases: Serverless Functions will create multiple database connections as traffic increases. All connections to the database need to be consumed quickly. To ensure uptime, consider using a serverless friendly database or connection pooling (https://vercel.com/docs/solutions/databases).

    (Source: Using Express.js with Vercel (https://vercel.com/guides/using-express-with-vercel))

These drawbacks highlight the importance of considering alternative approaches, such as using Next.js or embracing multiple Serverless Functions as you incrementally migrate to the Vercel platform.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@sturdy hornet kapa.ai is still learning and improving, please let us how it did by reacting below

sturdy hornet
#

@bold kraken alternative to expressjs standalone and also nextjs

bold krakenBOT
#

Let me check the knowledge base for you. One moment! :books:

#

If you're looking for an alternative to both Express.js and Next.js, you can consider using Vercel Serverless Functions (https://vercel.com/docs/concepts/functions) directly without any additional framework. This approach allows you to create lightweight, single-purpose functions that can handle specific API routes.

Here's an example of a simple Serverless Function:

  1. Create a file hello.ts inside the /api folder:
import { VercelRequest, VercelResponse } from '@vercel/node';

export default (req: VercelRequest, res: VercelResponse) => {
  res.status(200).send('Hello, world!');
};

This function will handle requests to /api/hello and respond with "Hello, world!".

  1. Deploy your project to Vercel by pushing it to a Git repository and importing it using your Git provider (https://vercel.com/import/git).

  2. After your project has been imported, all subsequent pushes to branches will generate Preview Deployments (https://vercel.com/docs/concepts/deployments/preview-deployments#), and all changes made to the Production Branch (https://vercel.com/docs/concepts/deployments/git#production-branch) (commonly "main") will result in a Production Deployment (https://vercel.com/docs/concepts/deployments/environments#production).

By using Vercel Serverless Functions directly, you can create a lightweight and scalable API without the need for a full-fledged framework like Express.js or Next.js.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

Vercel Documentation

Vercel Serverless Functions enable running code on-demand without needing to manage your own infrastructure, provision servers, or upgrade hardware.

Vercel Documentation

Preview Deployments allow you to preview changes to your app in a live deployment without merging those changes to your Git project's production branch.

Vercel Documentation

Environments are the solution for the deployment lifecycle on the Vercel platform. Learn about the production and preview environment types here.

Vercel Documentation

Vercel allows for automatic deployments on every branch push and merges onto the Production Branch of your GitHub, GitLab, and Bitbucket projects.

#

@sturdy hornet kapa.ai is still learning and improving, please let us how it did by reacting below

sturdy hornet
#

@bold kraken What about helmet, dotenv or cors support, how to handle that without expresses and vercel functions instead

bold krakenBOT
#

Let me check the knowledge base for you. One moment! :books: