-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
server.js
79 lines (61 loc) · 2.04 KB
/
server.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
/* eslint-disable no-console */
const _ = require('lodash')
const path = require('path')
const minimist = require('minimist')
const express = require('express')
const morgan = require('morgan')
const fruits = require('./fruits')
const app = express()
// get port from passed in args from scripts/start.js
const port = minimist(process.argv.slice(2)).port
app.use(morgan('dev'))
app.use(express.static('.'))
app.use('/node_modules', express.static(path.join(__dirname, '..', '..', 'node_modules')))
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/index.html`)
})
app.get('/redirect-example', (req, res) => {
res.sendFile(`${__dirname}/redirect-example.html`)
})
app.get('/local-api-example', (req, res) => {
res.sendFile(`${__dirname}/local-api.html`)
})
app.get('/form', (req, res) => {
res.sendFile(`${__dirname}/form.html`)
})
app.get('/fruits', (req, res) => {
res.sendFile(`${__dirname}/fruits.html`)
})
app.get('/fruits-jsonp', (req, res) => {
res.sendFile(`${__dirname}/fruits-jsonp.html`)
})
app.get('/favorite-fruits-jsonp', (req, res) => {
// we expect the query to have the function name "fruitsCallback"
// we need to call when the result gets back into the browser
console.log(req.query)
// return random 4 fruits for JSONP requests
const selectedFruits = _.sampleSize(fruits, 4)
const selectedFruitsJS = JSON.stringify(selectedFruits)
res.header('Content-Type', 'application/javascript')
res.header('Charset', 'utf-8')
res.send(`${req.query.fruitsCallback}(${selectedFruitsJS})`)
})
app.get('/favorite-fruits', (req, res) => {
res.json(_.sampleSize(fruits, 5))
})
app.get('/headers', (req, res) => {
res.sendFile(`${__dirname}/headers.html`)
})
app.get('/logout', (req, res) => {
console.log('logging out, redirecting to /')
res.redirect('/')
})
app.get('/getout', (req, res) => {
console.log('logging out, redirecting to www.cypress.io')
res.redirect('https://www.cypress.io')
})
app.get('/req-headers', (req, res) => {
console.log('request headers', req.headers)
res.json(req.headers)
})
app.listen(port)