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',
});Our official NodeJS server app type uses ESBuild by default and supports HMR for server development.
Create an Express server using the NodeJS env generator templates:
bit create express-server user-serverRun the server:
bit run user-serverSee an example for a simple Express server.
Create a GraphQL server using the NodeJS env generator template:
bit create graphql-server user-serverRun the server:
bit run user-serverYour new GraphQL server is now listening on the port shown in the output. You can see an example for a simple GraphQL server here.
The API server templates use our official Node Server type by default, and therefore use ESBuild to build your app.
bit build my-appIf using Ripple CI, snap and export your components. Build artifacts can be found in the directory the bit build command outputs.
├── my-app.cjsYou can now use Node to run your app:
node /path/to/output/my-app.cjsYou 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
});Learn about more options in the official Node Server on the NodeOptions docs.
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
};
}
});Use the deploy function to execute commands and use packages and APIs to deploy your backend service.
You can compose backend services into platforms using the Platform component. Head to Platforms to learn more about composing backend services into platforms.