robwormald / ng-universal-demo

256 stars 125 forks source link

Can not use other pages rather than main route #21

Closed mariohmol closed 7 years ago

mariohmol commented 7 years ago

Hi there!

Awesome example!

I'm trying to reproduce this but i cant make the routes to work, just the first route works ,I tryed using Component and the Modul, but boths gives me the same error:

Error: Unexpected value '[object Object]' imported by the module 'AppModule'. Please add a @NgModule annotation.
    at Error (native)

Example of paths:

 { path: '', component: HomeView, pathMatch: 'full'},
      { path: 'home/client', 
      loadChildren: './pages/home/home.module#HomeClientModule'},
      { path: 'home/seller', component: HomeSellerView},
      { path: 'home/commerce', component: HomeCommerceView, pathMatch: 'full'},

When I call a link with routerLink="/home/client" routerLinkActive="active" this works, changing the url to be /home/seller , but if i use the path directly (http://localhost/home/seller) it gives me this error

Any ideas?

Other thing is , what is the needed of server.aot , has this been call/used anywhere?


The full stack error

Error: Unexpected value '[object Object]' imported by the module 'AppModule'. Please add a @NgModule annotation.
    at Error (native)
    at syntaxError (/Users/projects/v2/web/dist/server.js:77963:34) [<root>]
    at /Users/projects/v2/web/dist/server.js:90430:44 [<root>]
    at Array.forEach (native) [<root>]
    at CompileMetadataResolver.getNgModuleMetadata (/Users/projects/v2/web/dist/server.js:90413:49) [<root>]
    at CompileMetadataResolver.getNgModuleSummary (/Users/projects/v2/web/dist/server.js:90355:52) [<root>]
    at /Users/projects/v2/web/dist/server.js:90428:72 [<root>]
    at Array.forEach (native) [<root>]
    at CompileMetadataResolver.getNgModuleMetadata (/Users/projects/v2/web/dist/server.js:90413:49) [<root>]
    at JitCompiler._loadModules (/Users/projects/v2/web/dist/server.js:101538:64) [<root>]
    at JitCompiler._compileModuleAndComponents (/Users/projects/v2/web/dist/server.js:101497:52) [<root>]
    at JitCompiler.compileModuleAsync (/Users/projects/v2/web/dist/server.js:101459:21) [<root>]
    at PlatformRef_._bootstrapModuleWithZone (/Users/projects/v2/web/dist/server.js:5092:25) [<root>]
    at PlatformRef_.bootstrapModule (/Users/projects/v2/web/dist/server.js:5078:21) [<root>]
    at View.engine (/Users/projects/v2/web/dist/server.js:70140:144) [<root>]
    at View.render (/Users/projects/v2/web/node_modules/express/lib/view.js:128:8) [<root>]
    at tryRender (/Users/projects/v2/web/node_modules/express/lib/application.js:640:10) [<root>]
    at EventEmitter.render (/Users/projects/v2/web/node_modules/express/lib/application.js:592:3) [<root>]
    at ServerResponse.render (/Users/projects/v2/web/node_modules/express/lib/response.js:966:7) [<root>]
    at /Users/projects/v2/web/dist/server.js:113289:13 [<root>]
    at Layer.handle [as handle_request] (/Users/projects/v2/web/node_modules/express/lib/router/layer.js:95:5) [<root>]
    at next (/Users/projects/v2/web/node_modules/express/lib/router/route.js:137:13) [<root>]
    at Route.dispatch (/Users/projects/v2/web/node_modules/express/lib/router/route.js:112:3) [<root>]
    at Layer.handle [as handle_request] (/Users/projects/v2/web/node_modules/express/lib/router/layer.js:95:5) [<root>]
    at /Users/projects/v2/web/node_modules/express/lib/router/index.js:281:22 [<root>]
    at Function.process_params (/Users/projects/v2/web/node_modules/express/lib/router/index.js:335:12) [<root>]
mariohmol commented 7 years ago

@MarkPieszak could you make your router to work?

mariohmol commented 7 years ago

Made from the scratch following this https://github.com/FrozenPandaz/ng-universal-demo and worked.

Had some issues with transfer state and material, but seems to be going to have on next version of universal 4.1

thanks

mariohmol commented 7 years ago

Actually.. i found the error. it happen when i change the webpack.server.js to have externals: [nodeExternals({ modulesFromFile: true })]

const { root } = require('./helpers');

const { AotPlugin } = require('@ngtools/webpack');
var nodeExternals = require('webpack-node-externals');

/**
 * This is a server config which should be merged on top of common config
 */
module.exports = {
  entry: root('./src/main.server.ts'),
  output: {
    filename: 'server.js'
  },
  target: 'node',
  externals: [nodeExternals({
      modulesFromFile: true
  })]
};

But i need this to make mongoose to work. If i remove this from webpack, and i try to do a mongoose.connect(DATABASE_URL, err => { i will receive this error

Error: Cannot find module "."
    at new Error (native)
    at webpackMissingModule (/Users/mariohmol/projetos/netgas/v2/web/dist/server.js:215729:56) [<root>]
mariohmol commented 7 years ago

Found that.. to work use

externals: [nodeExternals({
      modulesFromFile: true,
      modulesDir: './node_modules'
  })]