This repository has been archived by the owner on Jul 15, 2020. It is now read-only.
/
index.js
114 lines (98 loc) · 3.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict'
// Load environment variables from .env file if present
require('dotenv').load()
// now-logs allows remote debugging if deploying to now.sh
if (process.env.LOGS_SECRET) {
require('now-logs')(process.env.LOGS_SECRET)
}
// Default when run with `npm start` is 'production' and default port is '80'
// `npm run dev` defaults mode to 'development' & port to '3000'
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
process.env.PORT = process.env.PORT || 80
// Configure a database to store user profiles and email sign in tokens
// Database connection string for ORM (e.g. MongoDB/Amazon Redshift/SQL DB…)
// By default it uses SQL Lite to create a DB in /tmp/nextjs-starter.db
process.env.DB_CONNECTION_STRING = process.env.DB_CONNECTION_STRING || 'sqlite:///tmp/nextjs-starter.db'
// Secret used to encrypt session data stored on the server
process.env.SESSION_SECRET = process.env.SESSION_SECRET || 'change-me'
const express = require('express')
const next = require('next')
const orm = require('orm')
const auth = require('./routes/auth')
const app = next({
dir: '.',
dev: (process.env.NODE_ENV === 'development')
})
const handle = app.getRequestHandler()
let server
app.prepare()
.then(() => {
// Get instance of Express server
server = express()
// Set it up the database (used to store user info and email sign in tokens)
return new Promise((resolve, reject) => {
// Before we can set up authentication routes we need to set up a database
orm.connect(process.env.DB_CONNECTION_STRING, function (err, db) {
if (err) {
return reject(err)
}
// Define our user object
// * If adding a new oauth provider, add a field to store account id
// * Tokens are single use but don't expire & we don't save verified date
db.define('user', {
name: {type: 'text'},
email: {type: 'text', unique: true},
token: {type: 'text', unique: true},
verified: {type: 'boolean', defaultValue: false},
facebook: {type: 'text'},
google: {type: 'text'},
twitter: {type: 'text'}
})
// Creates require tables/collections on DB
// Note: If you add fields to am object this won't do that for you, it
// only creates tables/collections if they are not there - you still need
// to handle database schema changes yourself.
db.sync(function (err) {
if (err) {
return reject(err)
}
return resolve(db)
})
})
})
})
.then(db => {
// Once DB is available, setup sessions and routes for authentication
auth.configure({
app: app,
server: server,
user: db.models.user,
secret: process.env.SESSION_SECRET
})
// A simple example of a custom route
// Says requests to '/route/{anything}' will be handled by 'pages/routing.js'
// and the {anything} part will be pased to the page in parameters.
server.get('/route/:id', (req, res) => {
return app.render(req, res, '/routing', req.params)
})
// Default catch-all handler to allow Next.js to handle all other routes
server.all('*', (req, res) => {
return handle(req, res)
})
// Set vary header (good practice)
// Note: This overrides any existing 'Vary' header but is okay in this app
server.use(function (req, res, next) {
res.setHeader('Vary', 'Accept-Encoding')
next()
})
server.listen(process.env.PORT, err => {
if (err) {
throw err
}
console.log('> Ready on http://localhost:' + process.env.PORT + ' [' + process.env.NODE_ENV + ']')
})
})
.catch(err => {
console.log('An error occurred, unable to start the server')
console.log(err)
})