2017-06-05 47 views
0

我有一個簡單的Angular 4應用程序,我想在Angular Universal(nodeJs服務器端渲染)的服務器端運行。 我跟着these steps來配置Angular Universal和angular-cli的幫助,這一切都很好,直到我嘗試使用角度環境。 當我嘗試定它完全被在客戶端呈現(含伍服務)環境(ts-node src/server.ts)之內,但在服務器端的訪問屬性,它拋出以下錯誤:在Angular Universal運行在服務器端時找不到模塊環境

Error: Cannot find module 'environments/environment' 
    at Function.Module._resolveFilename (module.js:470:15) 
    at Function.Module._load (module.js:418:25) 
    at Module.require (module.js:498:17) 
    at require (internal/module.js:20:19) 
    at Object.<anonymous> (C:\Dev\code-carama\src\app\auth.service.ts:3:1) 
    at Module._compile (module.js:571:32) 
    at Module.m._compile (C:\Dev\code-carama\node_modules\ts-node\src\index.ts:385:23) 
    at Module._extensions..js (module.js:580:10) 
    at Object.require.extensions.(anonymous function) [as .ts] (C:\Dev\code-carama\node_modules\ts-node\src\index.ts:388:12) 
    at Module.load (module.js:488:32) 

npm ERR! Windows_NT 10.0.14393 
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start" 
npm ERR! node v7.10.0 
npm ERR! npm v4.2.0 
npm ERR! code ELIFECYCLE 
npm ERR! errno 1 
npm ERR! [email protected] start: `ts-node src/server.ts` 
npm ERR! Exit status 1 
npm ERR! 
npm ERR! Failed at the [email protected] start script 'ts-node src/server.ts'. 
npm ERR! Make sure you have the latest version of node.js and npm installed. 
npm ERR! If you do, this is most likely a problem with the code-carama package, 
npm ERR! not with npm itself. 
npm ERR! Tell the author that this fails on your system: 
npm ERR!  ts-node src/server.ts 
npm ERR! You can get information on how to open an issue for this project with: 
npm ERR!  npm bugs code-carama 
npm ERR! Or if that isn't available, you can get their info via: 
npm ERR!  npm owner ls code-carama 
npm ERR! There is likely additional logging output above. 

npm ERR! Please include the following file with any support request: 
npm ERR!  C:\Users\dima\AppData\Roaming\npm-cache\_logs\2017-06-05T12_00_03_595Z-debug.log 

這是我的server.ts

import 'reflect-metadata'; 
import 'zone.js/dist/zone-node'; 
import { platformServer, renderModuleFactory } from '@angular/platform-server'; 
import { enableProdMode } from '@angular/core'; 
import { AppServerModuleNgFactory } from '../dist/ngfactory/src/app/app.server.module.ngfactory'; 
import * as express from 'express'; 
import { readFileSync } from 'fs'; 
import { join } from 'path'; 

const PORT = 4000; 

enableProdMode(); 

const app = express(); 

let template = readFileSync(join(__dirname, '..', 'dist', 'index.html')).toString(); 

app.engine('html', (_, options, callback) => { 
    const opts = { document: template, url: options.req.url }; 

    //it serves html from the compiled angular app from the server 
    renderModuleFactory(AppServerModuleNgFactory, opts) 
    .then(html => callback(null, html)); 
}); 

app.set('view engine', 'html'); 
app.set('views', 'src') 

app.get('*.*', express.static(join(__dirname, '..', 'dist'))); 

app.get('*', (req, res) => { 
    res.render('index', { req }); 
}); 

app.listen(PORT,() => { 
    console.log(`listening on http://localhost:${PORT}!`); 
}); 

和簡單服務試圖訪問環境變量是auth.service.ts

import { Injectable } from '@angular/core'; 
import { environment } from 'environments/environment'; 

@Injectable() 
export class AuthService { 

    private userManager = null; 
    constructor() { 
    console.log('AuthService instantiated in environment ' + environment.envName); //this will fail in server-side rendering 
    } 
} 

任何想法與nodeJs(或角萬能)不發現該環境模塊?

感謝

回答

0

沒關係,看來問題是,儘管客戶端繪製行:

import { environment } from 'environments/environment';

效果很好,對服務器端渲染行必須是:

import { environment } from '../environments/environment';

順便說一句,我不知道爲什麼但默認情況下,服務器端渲染將選擇PROD環境文件。

控制檯上:AuthService instantiated in environment prod

相關問題