import 'zone.js/dist/zone-node' ; import { ngExpressEngine } from '@nguniversal/express-engine' ; import * as express from 'express' ; import { join } from 'path' ; import { APP_BASE_HREF } from '@angular/common' ; import { existsSync } from 'fs' ; import * as routeCache from 'route-cache' ; import * as jwtExpress from 'express-jwt' ; import * as cookieParser from 'cookie-parser' ; import * as compress from 'compression' ; import * as session from 'express-session' ; import * as cors from 'cors' ; import { AppServerModule } from './src/main.server' ; declare const __non_webpack_require__ ; const nodeRequire = __non_webpack_require__ ; const mainModule = __non_webpack_require__ . main ; const moduleFilename = mainModule && mainModule . filename || '' ; export function app ( ) { const CONFIG_FOLDER = join ( process . cwd ( ) , 'config' ) ; const config = nodeRequire ( join ( CONFIG_FOLDER , 'amazon.json' ) ) ; const server = express ( ) . disable ( 'x-powered-by' ) . use ( cookieParser ( ) ) ; const distFolder = join ( process . cwd ( ) , 'dist/browser' ) ; const indexHtml = existsSync ( join ( distFolder , 'index.original.html' ) ) ? 'index.original.html' : 'index' ; const SERVER_FOLDER = join ( process . cwd ( ) , 'server' ) ; const routes = nodeRequire ( join ( SERVER_FOLDER , 'routes.js' ) ) ; const insertions = nodeRequire ( join ( SERVER_FOLDER , 'new-insert' ) ) ; const authentication = nodeRequire ( join ( SERVER_FOLDER , 'authentication.js' ) ) ; server . use ( session ( { secret : config . session . secret , saveUninitialized : false , resave : true } ) ) ; server . use ( ( err , _ , res , next ) => { if ( err . name === 'StatusError' ) { res . send ( err . status , err . message ) ; } else { next ( err ) ; } } ) ; const jwtCheck = jwtExpress ( { algorithms : [ 'HS256' ] , secret : config . jwt . secret , getToken : ( req ) => { if ( req . headers . authorization && req . headers . authorization . split ( ' ' ) [ 0 ] === 'Bearer' ) { return req . headers . authorization . split ( ' ' ) [ 1 ] ; } if ( req . cookies && req . cookies . SIO_SESSION ) { return req . cookies . SIO_SESSION ; } return null ; } , } ) ; server . engine ( 'html' , ngExpressEngine ( { bootstrap : AppServerModule , } ) ) ; server . set ( 'view engine' , 'html' ) ; server . set ( 'views' , distFolder ) ; server . use ( express . json ( { limit : '5mb' } ) ) ; server . use ( express . urlencoded ( { extended : false } ) ) ; server . use ( cors ( ) ) ; server . use ( compress ( ) ) ; server . use ( '/api/user' , jwtCheck ) ; server . use ( '/auth' , authentication ( ) ) ; server . get ( '/api/customer' , routes . getCustomer ) ; server . get ( '/api/shop/:page' , routeCache . cacheSeconds ( 20 ) , routes . getAllProducts ) ; server . get ( '/api/product/:productId' , routeCache . cacheSeconds ( 86400 ) , routes . getProductById ) ; server . get ( '/api/user/cart' , routes . getFromCart ) ; server . get ( '/api/user/cart/count' , routes . getCartCount ) ; server . get ( '/api/user/checkout' , routes . getCheckoutInfo ) ; server . get ( '/api/user/orders' , routes . getUserOrders ) ; server . get ( '/api/user/order/:id' , routes . getOrderById ) ; server . get ( '/api/user/settings' , routes . getUserInfo ) ; server . delete ( '/api/user/cart/remove/:id' , routes . removeFromCart ) ; server . post ( '/api/user/checkout/confirm' , routes . checkoutConfirm ) ; server . post ( '/api/user/cart/add' , routes . addToCart ) ; server . post ( '/api/user/update' , routes . updateProfile ) ; server . post ( '/api/user/laptop' , insertions . newLaptop ) ; server . get ( '*.*' , express . static ( distFolder , { maxAge : '1y' } ) ) ; server . get ( '*' , ( req , res ) => { res . render ( indexHtml , { req , providers : [ { provide : APP_BASE_HREF , useValue : req . baseUrl } ] } ) ; } ) ; return server ; } function run ( ) { const port = process . env . PORT || 3000 ; const server = app ( ) ; server . listen ( port , ( ) => { console . log ( `Node Express server listening on http://localhost:${ port }` ) ; } ) ; } if ( moduleFilename === __filename || moduleFilename . includes ( 'iisnode' ) ) { run ( ) ; } export * from './src/main.server' ;