Advertisement
Guest User

new

a guest
Mar 21st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Copyright 2018, Google LLC.
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. //    http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13.  
  14. 'use strict';
  15.  
  16. const express = require('express');
  17. const app = express();
  18.  
  19. const path = require('path');
  20.  
  21. const bodyParser = require('body-parser');
  22.  
  23.  
  24. //Route stuff
  25. //Send main page
  26. var mainpage = express(); // the sub app
  27. mainpage.get('/', (req, res) => {
  28.   res.status(200).sendFile(path.join(__dirname + '/client/index.html'));  //Send file for mainpage
  29.   console.log("Served Main Page");
  30. });
  31. app.use('/mainpage', mainpage); //mainpage should use mainpage
  32.  
  33. //Redirect standard assets for main page
  34. var AppFolders = [  //What are the standard folders
  35.   '/css',
  36.   '/js',
  37.   '/index.html',
  38.   '/favicon.ico'
  39. ];
  40. AppFolders.forEach(path => {  //Send standard folders to their path in ./clinet
  41.   app.use(path, express.static('./client/' + path));
  42. });
  43.  
  44. //REST shit
  45. //app.use('/REST', bodyParser.json()); // support json encoded bodies
  46. //app.post('/REST/test', function(req, res) {
  47. //  var hey = req.body.hey;
  48. //  console.log(hey);
  49. //  res.send(hey);
  50. //});
  51.  
  52. //Send them to the mainpage from baseurl
  53. // and
  54. //Redirect vue-router pages on reload to the mainpage
  55. //    (needs to be last to not create redirect loups for paths defined above)   !!!!
  56. app.get('/*', (req, res) => {
  57.   res.redirect('/mainpage');
  58. });
  59.  
  60. if (module === require.main) {
  61.   // [START server]
  62.   // Start the server
  63.   const server = app.listen(process.env.PORT || 8080, () => {
  64.     const port = server.address().port;
  65.     console.log(`App listening on port ${port}`);
  66.   });
  67.   // [END server]
  68. }
  69.  
  70. module.exports = app;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement