Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* eslint no-console: 0 */
  2.  
  3. const path = require('path');
  4. const express = require('express');
  5. const webpack = require('webpack');
  6. const webpackMiddleware = require('webpack-dev-middleware');
  7. const webpackHotMiddleware = require('webpack-hot-middleware');
  8. const config = require('./webpack.config.js');
  9.  
  10. const isDeveloping = process.env.NODE_ENV !== 'production';
  11. const port = isDeveloping ? 3000 : process.env.PORT;
  12. var elasticsearch = require('elasticsearch');
  13. var client = new elasticsearch.Client({
  14.   host: 'localhost:9200',
  15.   log: 'trace'
  16. });
  17. const app = express();
  18.  
  19. if (isDeveloping) {
  20.   const compiler = webpack(config);
  21.   const middleware = webpackMiddleware(compiler, {
  22.     publicPath: config.output.publicPath,
  23.     contentBase: 'src',
  24.     stats: {
  25.       colors: true,
  26.       hash: false,
  27.       timings: true,
  28.       chunks: false,
  29.       chunkModules: false,
  30.       modules: false
  31.     }
  32.   });
  33.  
  34.   app.use(middleware);
  35.   app.use(webpackHotMiddleware(compiler));
  36.   app.get('*', function response(req, res) {
  37.     res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));
  38.     res.end();
  39.   });
  40. } else {
  41.   app.use(express.static(__dirname + '/img'));
  42.   app.get('*', function response(req, res) {
  43.     res.sendFile(path.join(__dirname, 'dist/index.html'));
  44.     res.json({ message: "I'm just testing to see if this works" });
  45.     res.send({"data": 1});
  46.     res.end();
  47.   });
  48. }
  49.  
  50.  
  51. app.get('/api/home', (req, res) => {
  52.   res.json({ message: "I'm just testing to see if this works" });
  53.   res.status(200);
  54.   res.end();
  55. });
  56.  
  57. app.get('/api/home', (req, res) => {
  58.   res.json({ message: "I'm just testing to see if this works" });
  59. });
  60.  
  61. app.listen(port, '0.0.0.0', function onStart(err) {
  62.   if (err) {
  63.     console.log(err);
  64.   }
  65.   console.info('==> 🌎 Listening on port %s. Open up http://0.0.0.0:%s/ in your browser.', port, port);
  66. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement