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 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 Node env generator template:

bit create express-server user-server
CopiedCopy

Run the server:

bit run user-server
CopiedCopy

Your new GraphQL server is now listening to 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 using ESBuild to build your app.

bit build my-app
CopiedCopy

If using Ripple CI, simply snap and export your components. Build artifacts can be found on the directory the bit build command will output.

├── my-app.cjs
CopiedCopy

You can now use node to simply 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 options set the 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: (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, 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 kearn more on composing backend services to platforms.

Learn more