Backend: Error Monitoring
Go
Java
JS
Python
Ruby
Backend: Logging
Go
JS
Python
Ruby
Java
Hosting Providers
Fullstack Frameworks
Overview
Self Host & Local Dev
Menu
Next.js Page Router Api
Installation
npm install @highlight-run/next
API route instrumentation
This section applies to Next.js Page Router routes only. Each Page Router route must be wrapped individually.
- Create a file to export your
PageRouterHighlight
wrapper function:
// utils/page-router-highlight.config.ts: import { CONSTANTS } from '../constants' import { PageRouterHighlight } from '@highlight-run/next/server' export const withPageRouterHighlight = PageRouterHighlight({ projectID: CONSTANTS.NEXT_PUBLIC_HIGHLIGHT_PROJECT_ID, })
- Wrap your
/pages/api
functions withwithPageRouterHighlight
:
// pages/api/nodejs-page-router-test.ts import { NextApiRequest, NextApiResponse } from 'next' import { withPageRouterHighlight } from '../../utils/page-router-highlight.config' export default withPageRouterHighlight(function handler(req: NextApiRequest, res: NextApiResponse) { console.info('Here: pages/api/nodejs-page-router-test.ts') if (req.url?.includes('error')) { throw new Error('Error: pages/api/nodejs-page-router-test.ts') } else { res.send('Success: pages/api/nodejs-page-router-test.ts') } })
Validation
- Run your app in dev mode with
npm run dev
.
- Copy/paste the above code snippet into
/pages/api/nodejs-page-router-test.ts
and hit the endpoint in your browser or withcurl
to watch it work.
curl http://localhost:3000/api/nodejs-page-router-test?error