Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. ...
  2. "start": "babel-node buildScripts/srcServer.js"
  3. ...
  4.  
  5. /* eslint-disable */
  6. import express from 'express'
  7. import path from 'path'
  8. import opn from 'opn'
  9. import bodyParser from 'body-parser'
  10. import historyApiFallback from 'connect-history-api-fallback'
  11. import WebpackDevMiddleware from 'webpack-dev-middleware'
  12. import WebpackHotMiddleware from 'webpack-hot-middleware'
  13. import WebpackConfig from '../webpack.config'
  14. import webpack from 'webpack'
  15.  
  16. const compiler = webpack(WebpackConfig)
  17. const app = express()
  18. const port = 8080
  19.  
  20. app.use(historyApiFallback({
  21. verbose: false
  22. }))
  23.  
  24. app.use(WebpackDevMiddleware(compiler, {
  25. publicPath: WebpackConfig.output.publicPath,
  26. hot: true,
  27. quiet: false,
  28. noInfo: false,
  29. lazy: false,
  30. stats: {
  31. colors: true
  32. }
  33. }))
  34.  
  35. app.use(WebpackHotMiddleware(compiler))
  36.  
  37. app.use(bodyParser.urlencoded({ extended: false}))
  38. app.use(bodyParser.json())
  39.  
  40. app.use('*', function (req, res, next) {
  41. console.log(req.url)
  42. var filename = path.join(compiler.outputPath,'login/login.html');
  43. compiler.outputFileSystem.readFile(filename, function(err, result){
  44. if (err) {
  45. return next(err);
  46. }
  47. res.set('content-type','text/html');
  48. res.send(result);
  49. res.end();
  50. });
  51. });
  52.  
  53. require('dotenv').config()
  54. const mongoose = require('mongoose')
  55. const User = require('../models/User')
  56.  
  57. mongoose.connect(process.env.DATABASE);
  58. mongoose.Promise = global.Promise;
  59. mongoose.connection
  60. .on('connected', () => {
  61. console.log(`Mongoose connection open on ${process.env.DATABASE}`);
  62. })
  63. .on('error', (err) => {
  64. console.log(`Connection error: ${err.message}`);
  65. });
  66. mongoose.model('User')
  67.  
  68. app.get('/users', function (req, res) {
  69.  
  70. User.find().lean().exec(function (err, users) {
  71. return res.end(JSON.stringify(users));
  72. })
  73.  
  74. })
  75.  
  76. app.get('/questions', function (req, res) {
  77. res.json([
  78. { 'id':1, 'header': 'Qheader', 'question': 'Question', 'description': 'Description' },
  79. { 'id':2, 'header': 'Qheader1', 'question': 'Question1', 'description': 'Description1' }
  80. ])
  81. })
  82.  
  83. // set up routing
  84. app.get('/', function (req, res) {
  85. res.sendFile(path.join(__dirname, '../dist/login/login.html'))
  86. })
  87.  
  88. app.get('/dashboard', function (req, res) {
  89. res.sendFile(path.join(__dirname, '../dist/dashboard/dashboard.html'))
  90. })
  91.  
  92. app.get('/assessments', function (req, res) {
  93. res.sendFile(path.join(__dirname, '../dist/assessments/assessments.html'))
  94. })
  95.  
  96. app.get('/registration', function (req, res) {
  97. res.sendFile(path.join(__dirname, '../dist/registration/registration.html'))
  98. })
  99.  
  100. app.get('/success', function (req, res) {
  101. res.sendFile(path.join(__dirname, '../dist/responses/success.html'))
  102. })
  103.  
  104. app.post('/registerUser', function (req, res) {
  105.  
  106. var user=new User({
  107. name:req.body.uname,
  108. uname:req.body.email,
  109. pass:req.body.pass
  110. })
  111.  
  112. user.save().then(item => {
  113. console.log('success')
  114. res.send("success")
  115. }).catch(err => {
  116. console.log('fail')
  117. res.status(400).send("failure")
  118. })
  119.  
  120. })
  121.  
  122. // set up listening
  123. app.listen(port, function (err) {
  124. if (err) {
  125. console.log(err)
  126. } else {
  127. opn('http://localhost:' + port)
  128. }
  129. })
  130.  
  131. const path = require('path')
  132. const webpack = require('webpack')
  133. const HtmlWebpackPlugin = require('html-webpack-plugin')
  134. const CopyWebpackPlugin = require('copy-webpack-plugin')
  135.  
  136. module.exports = {
  137. mode: 'development',
  138. entry: [
  139. 'webpack-hot-middleware/client?http://localhost:8080&timeout=20000', './src/login/app.js'
  140. ],
  141. target: 'web',
  142. output: {
  143. path: path.resolve(__dirname, 'dist'),
  144. publicPath: '/',
  145. filename: 'bundle.js'
  146. },
  147. plugins: [
  148.  
  149. new webpack.HotModuleReplacementPlugin(),
  150.  
  151. new CopyWebpackPlugin([
  152. {
  153. from: './src/images/',
  154. to: 'images/',
  155. force: true
  156. },
  157. {
  158. from: './node_modules/bootstrap/fonts/',
  159. to: 'node_modules/bootstrap/fonts/',
  160. force: true
  161. }
  162. ]),
  163.  
  164. new HtmlWebpackPlugin({
  165. filename: 'login/login.html',
  166. template: './src/login/login.html',
  167. inject: true
  168. }),
  169.  
  170. new HtmlWebpackPlugin({
  171. filename: 'dashboard/dashboard.html',
  172. template: './src/dashboard/dashboard.html',
  173. inject: true
  174. }),
  175.  
  176. new HtmlWebpackPlugin({
  177. filename: 'assessments/assessments.html',
  178. template: './src/assessments/assessments.html',
  179. inject: true
  180. }),
  181.  
  182. new webpack.ProvidePlugin({
  183. $: 'jquery',
  184. jQuery: 'jquery'
  185. })
  186. ],
  187. module: {
  188. rules: [
  189. {
  190. test: /.css$/,
  191. use: ['style-loader', 'css-loader']
  192. },
  193. {
  194. test: /.(eot|svg|ttf|woff|woff2)$/,
  195. use: {
  196. loader: 'file-loader',
  197. options: {
  198. name: '../../node_modules/bootstrap/fonts/[name].[ext]'
  199. }
  200. }
  201. },
  202. {
  203. test: /.(png|jpg)$/,
  204. use: {
  205. loader: 'file-loader',
  206. options: {
  207. name: '../images/[name].[ext]'
  208. }
  209. }
  210. },
  211. {
  212. test: /.(html)$/,
  213. use: {
  214. loader: 'html-loader',
  215. options: {
  216. name: '../*/[name].[ext]'
  217. }
  218. }
  219. }
  220. ]
  221. }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement