Skip to content

Commit

Permalink
add: node server and express
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-master committed Sep 17, 2018
1 parent e8c6686 commit dc5b453
Show file tree
Hide file tree
Showing 19 changed files with 8,599 additions and 64 deletions.
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
"scripts": {
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
"start": "npm run dev",
"lint": "eslint --ext .js,.vue src",
"server": "node ./server/index.js",
"lint": "eslint --ext .js,.vue src server",
"build": "node build/build.js"
},
"dependencies": {
"axios": "^0.18.0",
"element-ui": "^2.4.7",
"lodash": "^4.17.11",
"vue": "^2.5.2",
Expand All @@ -30,8 +32,12 @@
"babel-plugin-transform-vue-jsx": "^3.5.0",
"babel-preset-env": "^1.3.2",
"babel-preset-stage-2": "^6.22.0",
"body-parser": "^1.18.3",
"chalk": "^2.0.1",
"compression": "^1.7.3",
"copy-webpack-plugin": "^4.0.1",
"cors": "^2.8.4",
"cross-env": "^5.2.0",
"css-loader": "^0.28.0",
"eslint": "^4.15.0",
"eslint-config-standard": "^10.2.1",
Expand All @@ -42,6 +48,7 @@
"eslint-plugin-promise": "^3.4.0",
"eslint-plugin-standard": "^3.0.1",
"eslint-plugin-vue": "^4.0.0",
"express": "^4.16.3",
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "^1.1.4",
"friendly-errors-webpack-plugin": "^1.6.1",
Expand All @@ -56,6 +63,7 @@
"rimraf": "^2.6.0",
"semver": "^5.3.0",
"shelljs": "^0.7.6",
"spdy": "^3.4.7",
"uglifyjs-webpack-plugin": "^1.1.1",
"url-loader": "^0.5.8",
"vue-loader": "^13.3.0",
Expand Down
29 changes: 29 additions & 0 deletions server/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const _ = require('lodash')
const products = require('./mock/_products')

module.exports = function apiMiddleware(app) {
// returning products
app.get('/api/product/list', (req, res) => {
res.json({ payload: products }).end()
})

// get single product by sku
app.get('/api/product/:id', (req, res) => {
const id = parseInt(req.params.id, 10)
const product = _.find(products, { id: id })
if (!product) {
return res
.status(404)
.json({ error: { message: 'Product not found' } })
.end()
}
return res.json({ payload: product }).end()
})

app.all('/api/*', (req, res) =>
res
.status(404)
.json({ error: { message: 'Not found' } })
.end()
)
}
18 changes: 18 additions & 0 deletions server/certificate/cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-----BEGIN CERTIFICATE-----
MIIC9jCCAd6gAwIBAgIJAK0BJrHaPZgUMA0GCSqGSIb3DQEBBQUAMBYxFDASBgNV
BAMMCyoubG9jYWxob3N0MB4XDTE3MDYxODE5MjkxN1oXDTI3MDYxNjE5MjkxN1ow
FjEUMBIGA1UEAwwLKi5sb2NhbGhvc3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw
ggEKAoIBAQDJyQYsGGqVmAAbnnIE8VoXc79DEFDa+Lhw56WVFB7KOz6Y7a9+fJvW
UqrQaNQ5TWKG671XOy0ZUNAHuAowsHZTRFAgnjHtedq3DybBhy/jJtdAALQvg/rJ
hH1saEGr8u3eF00scBYusXjkj7u6DVFsvgPyWgXasTxV71HTDESuBMfi8yUQ4ceq
1d1LB6TOPMZXEJN8SsNO6oMzIj9aEpa0AP7IhujcheaZqDqA2echDCmYkMx8ozq/
xu8vxlVKtg8HRtCOt3evthr9NCZFZNmqDUVLCL0AVroRTBT4k7rEkN9Mtr4WKEwS
SAwoutSJKadhkwTRlKhWk/EvCj4O1PunAgMBAAGjRzBFMAsGA1UdDwQEAwIEMDAT
BgNVHSUEDDAKBggrBgEFBQcDATAhBgNVHREEGjAYggsqLmxvY2FsaG9zdIIJbG9j
YWxob3N0MA0GCSqGSIb3DQEBBQUAA4IBAQC/Kyxu6avtXPaaEQKuDc1Z6qO2SWis
CqTXr4lsuZTnK+RGI3N3upkh/WbKiVhLSqLP51WTyM+ekaW0Vjg8Ce7ph3deLVmF
WtNKmFqlUcEI4vsM8N2Tt+c5XRqlPfvNuLFlIdkj564cpF6oo8FvMI2LueXM+9N5
N6/c0wpjQyhYN80VmDET31rSmqFmj9NpmZUHeC/B8qOllawVGNVteMsHYP5kdmp1
LD0905T9umSWw0oCVW4HP6C8u2M6N03CkuvRZPbVjqf6porz5HRp+/NVK/3E3mfc
HX9Gineyo6RvwbsFCWQe2WHDEz9C6c/WPnbeG6nSoEpS8tA5JkYofUAi
-----END CERTIFICATE-----
28 changes: 28 additions & 0 deletions server/certificate/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDJyQYsGGqVmAAb
nnIE8VoXc79DEFDa+Lhw56WVFB7KOz6Y7a9+fJvWUqrQaNQ5TWKG671XOy0ZUNAH
uAowsHZTRFAgnjHtedq3DybBhy/jJtdAALQvg/rJhH1saEGr8u3eF00scBYusXjk
j7u6DVFsvgPyWgXasTxV71HTDESuBMfi8yUQ4ceq1d1LB6TOPMZXEJN8SsNO6oMz
Ij9aEpa0AP7IhujcheaZqDqA2echDCmYkMx8ozq/xu8vxlVKtg8HRtCOt3evthr9
NCZFZNmqDUVLCL0AVroRTBT4k7rEkN9Mtr4WKEwSSAwoutSJKadhkwTRlKhWk/Ev
Cj4O1PunAgMBAAECggEATGkMJkyEp+ssN0kzof2HARR6m7mk9JZ0uYRuplo9IpV2
qdBvwTUMigc52buoiZ5tLew9AbhN5qU8cX/WlANuey+GLCPbgjK/iMx6/5PipWDC
5+CcWtXedMrv98ou/B78xjoKpkY3kCzyO5LWIqkCaWw28GH4j4zH7T63vHSL2V0X
8YvRHcDh3BBiKMSv/XU2GY3S8um21uGNa8og/WrapL9DaKif73q7fAyJnjlge68N
7TjsKcfou2tZi/7iuQEGKZnV5BTP1w8r58jQR8jgfECtvqMMJgjjFLUL1oDL/JoP
OVFDKrj8aWCEDLjkTg7OOt+9of+O7k3xvxziL0UF+QKBgQDmlzzeMpnBViaz+qc6
d0hUu3RwCEwjpNh9bMgCgUnVebMQu122/97G20TF0yRMfCaeW11DY6CQv/L9p5YR
yP15aXPfhwBVkmC434BL2KCKNRcfWDsGs/gsc8Yt725louHNoKlTtHfaAlOR0RPG
yxfFWu1O7oyWEkz1cAt7M49w0wKBgQDgBTU+XMfL72y3haMeMwtj2XEeHMUolhXQ
x8/GXhfX2pEvdn4DWSTRoGlRg/PHVzHXZ+tdP+DlZyhTH5r9qVsX39KYlfgkdSDz
LFU1L1N3Wx6Vxo//IqHm5nZ0XOQSUwB4c7GaPuddK2+uk+KqruJghJ7gW8XI3OrL
C8RaB1ulXQKBgAQsRHu46rNm/bhQFuql8fcmlUrZu5cBirFqYIw7Dr0AQM9StLV1
F/D9UtdJpOSFcqV2/c8UZHl6H8mbWMnlm0CxOdmXtdfWmE2ALfAxcWZO46Y7kIXk
h982ESYPqS3JrtBr0eSj5W9aj2yaHgOA9WMq7BR7mKu2lRHz/yExo6zlAoGAeQFX
KUBki7vgNaFx4rYfyOeDOoi/1pR7ZDMKAFSC/I2LLuAKYTSdtr23JdjorRxkR75S
XCRNDp0SIxRHXA1GrzavQsx/JPI/whqITBosM6R8eNqwnVfPrDGAeopSh7Nd5bJY
lMC1qJX11Y78OMYOcaQxTW9E/qKWEOdiQF8JGeECgYBm9yty65kd3IsHmmnXz6PN
Hwaw5p3XTJkQT2uYM6enPrvslMVxEkl1JUMeG68XrglnJMdv/opnoA0XG5Ee02bw
m3Zc6kUsnSgQdnnncltbEptVAf8FNiOMz4z5lLeFpRLbYSTU6vo99yshDguMsgEd
gxqV07IHTfDpwclZsItWOw==
-----END PRIVATE KEY-----
57 changes: 57 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const fs = require('fs')
const path = require('path')
const spdy = require('spdy')
const cors = require('cors')
const express = require('express')
const bodyParser = require('body-parser')
const compression = require('compression')
const apiMiddleware = require('./api')

const app = express()

app.use(cors())
// Ensure secure
app.all('*', (req, res, next) => {
if (req.secure) return next()
res.redirect(
`https://${req.hostname}:${process.env.PORT_HTTPS || 3443}${req.url}`
)
})

// Setup body parsing
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())

apiMiddleware(app)

app.use(compression({ threshold: 0 }))

const https = spdy.createServer(
{
requestCert: false,
rejectUnauthorized: false,
key: fs.readFileSync(path.resolve(__dirname, './certificate/key.pem')),
cert: fs.readFileSync(path.resolve(__dirname, './certificate/cert.pem'))
},
app
)

https.listen(process.env.PORT_HTTPS || 3443, error => {
if (error) {
console.error(error)
return process.exit(1)
}
return console.log(
`App running in HTTPS on port: ${process.env.PORT_HTTPS || 3443}.`
)
})

app.listen(process.env.PORT || 8081, error => {
if (error) {
console.error(error)
return process.exit(1)
}
return console.log(
`App running in HTTP on port: ${process.env.PORT || 8081}.`
)
})
Loading

0 comments on commit dc5b453

Please sign in to comment.