1

Well, the question maybe has nothing to do with Angular 2, but...After watching Deborah's PluralSight Getting Started video, I tried to migrate my existing angular 1.x project to angular 2. For the client part, I want to switch to TypeScript and Angular 2, but for server part, I want to keep existing code as much as possible. The first issue I ran into is how to replace lite-server used in Deborah's project with my own server.js, while keeping her browsersync setup. Here is part of her package.json file which uses lite-server with -c parameter:

"scripts": {
    "build": "tsc -p src/",
    "build:watch": "tsc -p src/ -w",
    "serve": "lite-server -c=bs-config.json",
    "prestart": "npm run build",
    "start": "concurrently \"npm run build:watch\" \"npm run serve\"",
    "lint": "tslint ./src/**/*.ts -t verbose"
},

bs-config.json looks like this:

{
  "server": {
    "baseDir": "src",
    "routes": {
      "/node_modules": "node_modules"
    }
  }
}

My question is how to replace lite-server with my server.js which looks like this:

var express  = require('express');
var app      = express(); 
var morgan = require('morgan');
var bodyParser = require('body-parser');
var http = require('http');
var path = require('path');

var session = require('express-session');
var route = require('./server/route');

app.use(express.static(__dirname + '/public'));    
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));

var port = 3000;
app.listen(port);
console.log('App listening on port ' + port);

route.init(app);

app.get('*', function(req, res) {
   res.sendfile(__dirname + '/public/index.html');
});

systemjs.config.js

/**
 * System configuration for Angular 2 samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      // other libraries
      'rxjs': 'npm:rxjs'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}
newman
  • 6,841
  • 21
  • 79
  • 126

2 Answers2

0

you can use something like reload for express

reloadServer = reload(server, app);
watch.watchTree(__dirname + "/public", function (f, curr, prev) {
    // Fire server-side reload event
    reloadServer.reload();
});

it works with nodemoon, forever and node-supervisor, which is the recommended way of using reload.

KB_
  • 2,113
  • 2
  • 26
  • 28
  • I tried it based on the link, the server side is okay (I mean no error), but the client side throws this error "Uncaught ReferenceError: require is not defined at reload.js:1" when I include reload.js in my index.html as instructed. Any idea? – newman Apr 01 '17 at 04:17
0

Update Package.json. Run npm install again.Then npm start.

"scripts": {

"start": "node server/server.js"
}

"server/server.js" is path of server.js file.

Vivek
  • 1,375
  • 2
  • 15
  • 24
  • my server.js is in root, so I can run it as "node server.js". The server started but the client stuck at "Loading...", because, I believe, the paths are inconsistently defined. In browser log, I see something like "Error: (SystemJS) Unexpected token <". – newman Apr 01 '17 at 04:00
  • ok, I made the project up running by copying node_modules into public subfolder because otherwise, angularjs dependencies can't be found in systemjs via paths: { 'npm': 'node_modules/'}. I tried to change it to paths: { 'npm': './../node_modules/'}, but it didn't help. Any clue? – newman Apr 01 '17 at 04:07
  • Can you share your package.json & tsconfig.json or webpack(if you use it)? Do you use Angular CLI or Webpack? – Vivek Apr 01 '17 at 04:22
  • http://stackoverflow.com/questions/34387174/angular-2-quickstart-unexpected-token – Vivek Apr 01 '17 at 04:29
  • I updated my questions with tsconfig.json and systemjs.config.js. – newman Apr 01 '17 at 14:00