Skip to content

Instantly share code, notes, and snippets.

@josiahwiebe
Created February 9, 2018 15:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save josiahwiebe/a61fc078ef93e9437fb4340213866ef7 to your computer and use it in GitHub Desktop.
Save josiahwiebe/a61fc078ef93e9437fb4340213866ef7 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>
)
@lukeed
Copy link

lukeed commented Feb 9, 2018

Actually, this works:

  const server = polka({ onNoMatch:handle })

  server.use('/api', api)
  server.get('/a', (req, res) => app.render(req, res, '/a', req.query))

Do you think that's too funky to use? I mean, get('*') is a catch-all anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment