-
-
Save bogordesaincom/7a537b1fa6cb8bb55e4c9f4402e80394 to your computer and use it in GitHub Desktop.
Polka NextJS + API Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const polka = require('polka') | |
module.exports = polka() | |
.get('/', (req, res) => { | |
res.end('this is the api') | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
export default () => <div>a</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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