Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var express = require('express');
var ng2engine = require('angular2-universal-preview').ng2engine;
// Angular 2
var App = require('./dist/server/app').App;
var app = express();
// Express View
app.engine('.ng2.html', ng2engine);
app.set('views', __dirname);
app.set('view engine', 'ng2.html');
// static files
app.use(express.static(__dirname));
app.use('/', function(req, res) {
res.render('index', {App: App});
});
app.listen(3000, function() {
console.log('Listen on http://localhost:3000');
});
return through.obj(function transform(file, enc, cb) {
if (file.isStream()) {
return cb(new PluginError('angular2-gulp-prerender', 'Streaming is not supported'));
}
var str = file.contents.toString();
var renderPromise = universal.renderToString;
var args = [config.App, config.providers];
if (config.preboot) {
renderPromise = universal.renderToStringWithPreboot;
args.push(config.preboot);
}
renderPromise.apply(null, args)
.then(function(serializedApp) {
var html = str.replace(
//
universal.selectorRegExpFactory(universal.selectorResolver(config.App)),
// {{ serializedCmp }}
serializedApp
);
file.contents = new Buffer(html);
return through.obj(function transform(file, enc, cb) {
if (file.isStream()) {
return cb(new PluginError('angular2-gulp-prerender', 'Streaming is not supported'));
}
var str = file.contents.toString();
var renderPromise = universal.renderToString;
var args = [config.App, config.providers];
if (config.preboot) {
renderPromise = universal.renderToStringWithPreboot;
args.push(config.preboot);
}
renderPromise.apply(null, args)
.then(function(serializedApp) {
var html = str.replace(
//
universal.selectorRegExpFactory(universal.selectorResolver(config.App)),
// {{ serializedCmp }}
serializedApp
);
file.contents = new Buffer(html);
cb(null, file)
});
.then(function(serializedApp) {
var html = str.replace(
//
universal.selectorRegExpFactory(universal.selectorResolver(config.App)),
// {{ serializedCmp }}
serializedApp
);
file.contents = new Buffer(html);
cb(null, file)
});
});
renderToString: function(callback, options) {
try {
var component = findAngularComponent(options);
var serverBindings = [
ngRouter.ROUTER_BINDINGS,
ngUniversal.HTTP_PROVIDERS,
ngCore.provide(ngUniversal.BASE_URL, { useValue: options.requestUrl }),
ngCore.provide(ngRouter.APP_BASE_HREF, { useValue: '/' }),
ngUniversal.SERVER_LOCATION_PROVIDERS
];
return ngUniversalRender.renderToString(component, serverBindings).then(
function(successValue) { callback(null, successValue); },
function(errorValue) { callback(errorValue); }
);
} catch (synchronousException) {
callback(synchronousException);
}
}
};
renderToString: function(callback, options) {
try {
var component = findAngularComponent(options);
var serverBindings = [
ngRouter.ROUTER_BINDINGS,
ngUniversal.HTTP_PROVIDERS,
ngCore.provide(ngUniversal.BASE_URL, { useValue: options.requestUrl }),
ngCore.provide(ngRouter.APP_BASE_HREF, { useValue: '/' }),
ngUniversal.SERVER_LOCATION_PROVIDERS
];
return ngUniversalRender.renderToString(component, serverBindings).then(
function(successValue) { callback(null, successValue); },
function(errorValue) { callback(errorValue); }
);
} catch (synchronousException) {
callback(synchronousException);
}
}
};
renderToString: function(callback, options) {
try {
var component = findAngularComponent(options);
var serverBindings = [
ngRouter.ROUTER_BINDINGS,
ngUniversal.HTTP_PROVIDERS,
ngCore.provide(ngUniversal.BASE_URL, { useValue: options.requestUrl }),
ngCore.provide(ngRouter.APP_BASE_HREF, { useValue: '/' }),
ngUniversal.SERVER_LOCATION_PROVIDERS
];
return ngUniversalRender.renderToString(component, serverBindings).then(
function(successValue) { callback(null, successValue); },
function(errorValue) { callback(errorValue); }
);
} catch (synchronousException) {
callback(synchronousException);
}
}
};
renderToString: function(callback, options) {
try {
var component = findAngularComponent(options);
var serverBindings = [
ngRouter.ROUTER_BINDINGS,
ngUniversal.HTTP_PROVIDERS,
ngCore.provide(ngUniversal.BASE_URL, { useValue: options.requestUrl }),
ngCore.provide(ngRouter.APP_BASE_HREF, { useValue: '/' }),
ngUniversal.SERVER_LOCATION_PROVIDERS
];
return ngUniversalRender.renderToString(component, serverBindings).then(
function(successValue) { callback(null, successValue); },
function(errorValue) { callback(errorValue); }
);
} catch (synchronousException) {
callback(synchronousException);
}
}
};