sails.config.XXXX in app.js for SailsJS Framework - node.js

I am using Sails Framework with the newrelic plugin. I am currently trying to register the app based on a flag in production.js/development.js. This is the flag in development.js:
ENABLE_NEWRELIC_NODE_SERVICE: false,
And this is the piece of code in App.js:
if (sails.config.ENABLE_NEWRELIC_NODE_SERVICE){
require('sails-hook-newrelic/register');
}
But it seems that sails.config is not accessible in app.js (I might be wrong). Is there any other way to include conditionality in App.js based on config files?
Thanks!

Short of a Sails specific way of doing this, simply require in the config file:
var config = require('./sails.config') // Check the path is correct
Then use the flag:
if (config.ENABLE_NEWRELIC_NODE_SERVICE){
require('sails-hook-newrelic/register');
}

The sails.config object isn't available until Sails is actually loaded. Looking at the code of sails-hook-newrelic, it's not clear to me why register() needs to be called so early; it seems like it could just happen as part of the hook initialization. But, the code isn't written that way, so you're stuck on that point. Looks like there's a pull request open to fix it. In the meantime, assuming that you want to activate New Relic based on the Node environment, you can just check process.NODE_ENV in your app.js:
if (process.NODE_ENV == 'development') {
require('sails-hook-newrelic/register');
}
Just make sure that you start your app with NODE_ENV=development node app.js.

Related

Use javaScript file in angular 5 project

I am building a web application using Angular 5 and nodejs with express. As both the frontend and the backend are going to run in the same server I want to use my backend javascript functions in the frontend. The solutions that I have found didn't worked for me.
appController.js
var createApp = function (appData) {
console.log("App created")
}
exports.createApp = createApp;
This is the backend file that I want to use in the front.
Javascript files don't need to export things when used in the front-end. Most of the time, they use global variables. You should go with that.
To add it to your project, add it anywhere you want, and in your Typescript, you can simply use
declare var myGlobalVariable: any;
// ...
myGlobalVariable.createApp();
I did a similar thing. I have a frontend with Angular 5 and a backend serving an API via Express:
If you want to run your Angular frontend and your NodeJS backend on the same server you'll have to build your Angular project and serve the built files with your Express server.
I think I found a tutorial on that – but I can't find it right now...
Instead maybe have a look at my code for the backend:
https://github.com/saitho/ngHashi/blob/next/backend/src/index.ts
In production mode it will serve the files in the folder /backend/dist_frontend by default (apart from the backend routes (/api)). I use a build process (GitLab CI) to move the files I need to the respective place...
I finally find a solution. It would be necessary to modify the tsconfig.json:
tsconfig.json
{
compilerOptions: {
.....
allowJS: true,
......
}
}

How to use CAS authentication with angular2 webpack starter typescript?

I am using the following:
https://github.com/gdi2290/angular-starter
I start the application with npm start that uses webpack dev server. My issue is I want to add CAS authentication, but have no idea where what goes. This is the library I am trying to use:
https://github.com/TencentWSRD/connect-cas2
All the examples appear to use express, though I am not sure if I can use this with the webpack-dev-server as is with the starter I am using? If so, how do I use it? Or do I need a different CAS library that is compatible with the npm start?
Webpack is more of a build tool with minimal server capability. It can serve static content making it perfect for updating content on the fly and put together simple websites with basic routing and client side side logic.
If you want anything even a little more complex you will need a server side technology. So the connect-cas2 will require you to use node.js and express. The documentation shows you exactly how to set it up.
I don't know anything about connect-cas2 but I know passport for authentication, but they wouldn't be terribly different. I would follow the quick start and if you have any issues then edit your question with the code that you are having issues with or have trouble understanding.
I have been digging around and you might be able to proxy the authentication if you already have a CAS server setup somewhere. I would try the following:
// the proxy option is part of the devServer in your webpack.config.js
devServer: {
contentBase: 'public',
// setup a proxy
proxy: {
// just replace the /cas/validate endpoint to the endpoint in your
// website that will trigger the api call
'/cas/validate': 'https://url-to-your-cas-server.com'
}
},

Webpack somehow bypasses Express routing completely

I am starting from this excellent tutorial: https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/ and trying to extend it by adding a new page to serve through a new route. However after hours of mucking around I am realizing that somehow create-react-app is doing some weird magic (as mentioned in their docs here):
`create-react-app` configures a Webpack development server to run on `localhost:3000`.
This development server will bundle all static assets located under `client/src/`.
All requests to `localhost:3000` will serve `client/index.html` which will include Webpack's `bundle.js`.
The key quote is "All requests to localhost:3000 will serve client/index.html". I have no idea how this happens. So even though i mess around with routes/index.js:
app.route('/')
.get(function (req, res) {
res.sendFile(bipath.join(__dirname, '../public', 'THISCANBEANYRANDOMFILENAME.html'))
});
it doesnt matter because webpack is somehow directing localhost:3000 to index.html anyway. where and how is it doing this? Bottom line I am trying to modify my routes to serve a new html file and am running into all sorts of filepath issues (yes, even when i use require('path') or sendFile(...,{root: __dirname}).)
So what exactly is going on here and can you give me any hints to help me out?
Edit: this could be from babel as well as webpack - i'm not exactly clear where babel hands off and where webpack starts.
I haven't played around with create-react-app, but it seems like instead of using the default npm start, you could create your own server file and run that.
This looks like a good example.
https://medium.com/#patriciolpezjuri/using-create-react-app-with-react-router-express-js-8fa658bf892d#.6y4rrl61q
Alternatively, if you're looking to have routes used as an api, you could proxy them to a different port like shown in the tutorial you linked.

Webpack-dev-server and isomorphic react-node application

I've managed to properly use webpack dev server alongside with a node server (express), using the plugin section inside webpack's config.
It all works fine but now I'm trying to go isomorphic and use client-side components inside the express application.
So far the only problem I'm encountering is that without webpack 'parsing' my server-side code I get to a situation where I require components but the paths are not solved
I.E.
Inside a component
'use strict';
import React from 'react';
import { RouteHandler, Link } from 'react-router';
import Header from 'components/header/main'; // <-- This line causes the error because webpack is not working when parsing this JSX server-side
export default React.createClass({
displayName: 'App',
render() {
return ( // ... More code
Shall I configure webpack in another way or do I have to change all the imports to be valid server-side?
the codebase is here in case you want to see the actual state https://github.com/vshjxyz/es6-react-flux-node-quickstart
In order to be able to require components in a way such as require('components/Header.js'); and avoid using long relative paths such as require('../../../../../../Header.js'); you can add this code to your node app before any require() calls:
process.env.NODE_PATH = __dirname;
require('module').Module._initPaths();
However, since this relies on a private Node.js core method, this is
also a hack that might stop working on the previous or next version of
node.
Other possible solutions to this problem can be found at https://gist.github.com/branneman/8048520
I see 2 options:
Compile client code with webpack as well. If client's entry
point is in the same dir as server's - it should work with your
present code. This looks natural to me.
Use relative paths i.e.
import Header from './components/header/main'

All requests via Proxy in Node-Webkit

Is there a way to force Node-webkit to use proxied settings for all requests ? I know there is an API solution using setProxyConfig() but this doesn't work with authenticated proxy, the prompt shows up to login but crash when submitted...
So I tried to use node request module, and it works fine :
var request=require('request');
var proxy = request.defaults({'proxy':'http://login:pwd#proxy:port'});
But now my problem is to tell node-webkit to use this for each request.
Any solutions ?
I'm quite new using node and node-webkit so maybe there is a better way to do that.
Thanks a lot !
You may try
process.env.http_proxy = 'http://login:pwd#proxy:port'
It will work with request lib, also should impact other requests from node-webkit (assets, ajax)
But may be other libraries don't use environment proxy settings. If that doesn't work you can try https://www.npmjs.com/package/global-tunnel
Not sure if this is solved, but i came up with a simple solution:
i wrote a simple wrapper for the request library
https://gist.github.com/jodevsa/7dd9662b8244359fa0d7626ae7c9bd69
all you have to do is ,
Head to $PROJECT_DIR/node_modules/request/
Rename index.js to core.js
Create a new file called index.js
Copy code content from the above gist link to index.js
Change proxyLoc value to you'r preferred proxy
If you decided to disable proxy , just change value of ON variable to false inside index.js
Cheers :D

Resources