vyakymenko / angular-seed-express

[DEPRECATED, Please use https://github.com/vyakymenko/angular-express] Extensible, reliable and modular starter project for Angular 7 with statically typed build AoT compilation, Express server and PM2 Daemon.

Home Page:https://github.com/vyakymenko/angular-seed-express

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HTTP/2

NiLeShpatil777 opened this issue · comments

hello,I am trying to use Http/2 in project.i have mongodb as database.i have installed spdy .has any one implimented it.needs some help...

Hello @NiLeShpatil777 , what problems with current seed do you have? Becaus all that I understand for now, you have problems and help. With what ?

The current project is running perfectly awesome including debugging too.In order to get benefits of http2 i wanted to implement the same.I have created localhost ssl certificate using "localhost-ssl".The requests are going through http2 but for server push feature of http2 what changes in file should be done .Can server push feature help us to load index.html of our project in better way.my code of index.ts file is as follows,

import * as http from 'http';
import * as express from 'express';
import * as bodyParser from 'body-parser';
import * as path from 'path';
import * as compression from 'compression';
import * as routes from './routes';
import * as methodOverride from 'method-override';
import * as cors from 'cors';
import * as fs from 'fs';
var spdy = require('spdy');
var _clientDir = '../client';
var app = express();

export function init(port: number, mode: string) {

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(bodyParser.text());
app.use(compression());
app.use(cors());
app.use(bodyParser.json());
app.use(methodOverride("X-HTTP-Method"));
app.use(methodOverride("X-HTTP-Method-Override"));
app.use(methodOverride("X-Method-Override"));
app.use(methodOverride("_method"))
app.use(express.static('src/'));
/**

  • Dev Mode.

  • @note Dev server will only give for you middleware.
    /
    if (mode == 'dev') {
    app.all('/
    ', function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'X-Requested-With');
    next();
    });

    routes.init(app);

    let root = path.resolve(process.cwd());
    let clientRoot = path.resolve(process.cwd(), './dist/dev/client');
    app.use(express.static(root));
    app.use(express.static(clientRoot));

    var renderIndex = (req: express.Request, res: express.Response) => {
    res.sendFile(path.resolve(__dirname, _clientDir + '/index.html'));
    };
    app.get('/*', renderIndex);

    /**

    • Api Routes for Development.
      /
      }
      else {
      /
      *

    • Prod Mode.

    • @note Prod mod will give you static + middleware.
      */

      /**

    • Api Routes for Production.
      /
      routes.init(app);
      /
      *

    • Static.
      */
      app.use('/js', express.static(path.resolve(__dirname, _clientDir + '/js')));
      app.use('/css', express.static(path.resolve(__dirname, _clientDir + '/css')));
      app.use('/assets', express.static(path.resolve(__dirname, _clientDir + '/assets')));

      /**

    • Spa Res Sender.

    • @param req {any}

    • @param res {any}
      _/
      var renderIndex = function (req: express.Request, res: express.Response) {
      res.sendFile(path.resolve(__dirname, clientDir + '/index.html'));
      };
      //app.get('
      ', function(req,res) {
      // res.sendFile(process.cwd() + '/dist/prod/client/index.html');
      //});

      /**

    • Prevent server routing and use @ng2-router.
      /
      app.get('/
      ', renderIndex);
      }

    /**

  • Server with gzip compression.
    */
    //HTTP2 start
    const options = {
    key: fs.readFileSync("./server.key"),
    cert: fs.readFileSync("./server.crt")
    }
    return server = spdy.createServer(options, app)
    .listen(port, (error:any) => {
    if (error) {
    console.error("index file error http2",error)
    return process.exit(1)
    } else {
    console.log('HTTP2 App Listening on port: ' + port + '.')
    resolve(server);
    }
    });
    //HTTP2 End
    };

@NiLeShpatil777 , excuse me, but it's a specific task just for you and I don't see any reason to add this feature ;) This seed just a starter pack for your angular-seed-express project.