forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (49 loc) · 1.7 KB
/
server.js
File metadata and controls
59 lines (49 loc) · 1.7 KB
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
import express from 'express'
import cors from 'cors'
import path from 'path'
import { LDP } from './ldp.js'
import { ResourceMapper } from './resource-mapper.js'
import { createMiddleware } from './middleware.js'
import { HttpError } from './error.js'
import { debug } from './utils.js'
export function createApp (options = {}) {
const {
root = process.cwd(),
serverUri = 'https://localhost:8443',
skipAuth = false
} = options
const rootPath = path.resolve(root)
const rootUrl = serverUri.endsWith('/') ? serverUri.slice(0, -1) : serverUri
const mapper = new ResourceMapper({ rootUrl, rootPath })
const ldp = new LDP({ rootPath, rootUrl, mapper })
const app = express()
// CORS
app.use(cors({
methods: ['OPTIONS', 'HEAD', 'GET', 'PATCH', 'POST', 'PUT', 'DELETE'],
exposedHeaders: 'Authorization, User, Location, Link, Vary, Last-Modified, ETag, Accept-Patch, Accept-Post, Accept-Put, Updates-Via, Allow, WAC-Allow, Content-Length, WWW-Authenticate',
credentials: true,
maxAge: 1728000,
origin: true,
preflightContinue: true
}))
// Common headers
app.use((req, res, next) => {
res.set('X-Powered-By', 'solid-server/7.0.0')
res.set('Vary', 'Accept, Authorization, Origin')
next()
})
// LDP middleware
app.use('/', createMiddleware({ ldp, rootUrl, skipAuth }))
// Error handler
app.use((err, req, res, _next) => {
if (err instanceof HttpError) {
debug(`${err.status} ${err.message} — ${req.method} ${req.path}`)
res.status(err.status).send(err.message)
} else {
debug(`500 ${err.message} — ${req.method} ${req.path}`)
res.status(500).send('Internal Server Error')
}
})
app.locals.ldp = ldp
return app
}