Creating Backend Services

You can use our official NodeJS app server type and templates to generate GraphQL and Express application templates baked with best practices.

Use the Node Server type in your Bit app to define a new backend service:

import { NodeServer } from '@bitdev/node.node-server';

export default NodeServer.from({
  name: 'corporate-service',
  mainPath: './corporate-service.app-root.js',
});
CopiedCopy

Our official NodeJS server app type uses ESBuild by default and supports HMR for server development.

Express

Create an Express server using the NodeJS env generator templates:

bit create express-server user-server
CopiedCopy

Run the server:

bit run user-server
CopiedCopy

See an example for a simple Express server.

GraphQL

Create a GraphQL server using the NodeJS env generator template:

bit create graphql-server user-server
CopiedCopy

Run the server:

bit run user-server
CopiedCopy

Your new GraphQL server is now listening on the port shown in the output. You can see an example for a simple GraphQL server here.

Build and deploy

The API server templates use our official Node Server type by default, and therefore use ESBuild to build your app.

bit build my-app
CopiedCopy

If using Ripple CI, snap and export your components. Build artifacts can be found in the directory the bit build command outputs.

├── my-app.cjs
CopiedCopy

You can now use Node to run your app:

node /path/to/output/my-app.cjs
CopiedCopy

Single executable file

You can use the Node Server to build the server into a NodeJS executable file. To enable this option, set binary to true:

import { NodeServer } from '@bitdev/node.node-server';

export default NodeServer.from({
  name: 'corporate-service',
  mainPath: './corporate-service.app-root.js',
  binary: true
});
CopiedCopy

Learn about more options in the official Node Server on the NodeOptions docs.

Deploy

You can deploy backend services using the deploy function provided to the Node Server:

import { NodeServer } from '@bitdev/node.node-server';

export default NodeServer.from({
  name: 'corporate-service',
  mainPath: './corporate-service.app-root.js',
  deploy: async (context: AppDeployContext) => {
    const artifact = context.artifacts.findByName('app-bundle');
    const path = artifact.artifactDir;
    const url = execSync(`deploy ${path}`).toString('utf-8');
    
    return {
      url
    };
  }
});
CopiedCopy

Use the deploy function to execute commands and use packages and APIs to deploy your backend service.

Compose in platforms

You can compose backend services into platforms using the Platform component. Head to Platforms to learn more about composing backend services into platforms.

Learn more