Skip to content

Instantly share code, notes, and snippets.

@bogordesaincom
Forked from josiahwiebe/api.js
Created October 10, 2022 16:13
Show Gist options
  • Save bogordesaincom/7a537b1fa6cb8bb55e4c9f4402e80394 to your computer and use it in GitHub Desktop.
Save bogordesaincom/7a537b1fa6cb8bb55e4c9f4402e80394 to your computer and use it in GitHub Desktop.
Polka NextJS + API Example
const polka = require('polka')
module.exports = polka()
.get('/', (req, res) => {
res.end('this is the api')
})
const polka = require('polka')
const next = require('next')
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const api = require('./api')
app.prepare().then(() => {
const server = polka()
server.use('/api', api)
server.get('/a', (req, res) => app.render(req, res, '/a', req.query))
server.get('*', (req, res) => handle(req, res))
server.listen(port).then(() => {
console.info(`> Next.js is running on http://localhost:${port}`)
})
})
import React from 'react'
export default () => <div>a</div>
import React from 'react'
import Link from 'next/link'
export default () => (
<ul>
<li>
<Link href='/a'>
<a>a</a>
</Link>
</li>
</ul>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment