1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/with-cookie-auth/www/server.js
Juan Olvera 80cb91ec87 Add setup to run example with cookie authentication locally (#6101)
* extract request login from auth

* add clarification that the monorepo is for deploy in Now only and fix typo

* Refactor HOC

- add authorization to HOC
- add displayName to HOC
- remove unnecessary `run`s in local routing
2019-02-11 14:17:43 +01:00

50 lines
1.2 KiB
JavaScript

const { createServer } = require('http')
const httpProxy = require('http-proxy')
const { parse } = require('url')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const proxy = httpProxy.createProxyServer()
const target = 'http://localhost:3001'
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
switch (pathname) {
case '/':
app.render(req, res, '/', query)
break
case '/login':
app.render(req, res, '/login', query)
break
case '/api/login.js':
proxy.web(req, res, { target }, error => {
console.log('Error!', error)
})
break
case '/profile':
app.render(req, res, '/profile', query)
break
case '/api/profile.js':
proxy.web(req, res, { target }, error => console.log('Error!', error))
break
default:
handle(req, res, parsedUrl)
break
}
}).listen(3000, err => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})