Member-only story
Node.js Best Practices — Error Handling and Logging
Node.js is a popular runtime to write apps in JavaScript. To make maintaining them easier, we’ve to set some guidelines for people to follow.
In this article, we’ll look at how to document APIs and gracefully exit processes.
Document API errors using GraphQL
We can build our API using GraphQL libraries. This provides us with a sandbox for querying data. It also provides strong typing and only returns what we want.
It also provides schema and comments so that we can document our APIs without adding more documentation.
To create a GraphQL API, we can use the graphql
and express-graphql
packages to create a GraphQL API. We install it by running:
npm i express-graphql graphql
Then we can create a simple GraphQL API by writing:
const express = require('express');
const bodyParser = require('body-parser');
const graphqlHTTP = require('express-graphql');
const { buildSchema } = require('graphql');
const app = express();app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));const schema = buildSchema(`
type Query {
quoteOfTheDay: String
}
`);