Environment variable in Javascript (Gulp) - javascript

I have a local development machine and a test server.
Now I have an APP_ID that's being used in Javascript. I've been looking into ways that they kan differ on my local machine and on the test server.
Using Gulp
With gulp it's possible to add a flag on the command line:
gulp build --env=production
That way I can get the correct APP_ID from a file.
The only issue is with this approach I need to run my build on the server, at this moment I run gulp locally and upload the changes to my server
Is it okay to build on the server? Are there other ways to use environment variables in Javascript?

My suggestion is to not build on the server but build locally and then deploy to the server using one of the many deploy solution (es. deployer.org for php). Normally javascript NPM packages put build output even in GIT repository ready for use in other projects or for deploy.
For more info on how to use env variable in node (gulp run over node) see this page
For example in linux you can set env variable with export
app.js :
console.log(console.log(process.env.foo))
Then try
> export foo=app1
> node app.js
Res:
app1
Then try
> export foo=app2
> node app.js
Res:
app2
This is valid only if you run your code server side on node (ex on gulp).
If you are developing a client side library and you want to create different builds that targets different enviroments you have to instruct gulp to do so. In this case this is a guide that can help you

Related

Deploy node.js / express / mongoDB ( with typescript ) in heroku

Before, in the backend i would not use typescript, i would just use pure javascript, but know, this is my first backend app with typescript.
I want to deploy of course this app to heroku, but, i wonder if in the procfile component i have to tell heroku to get the server running in the server.ts, or in the server.js in the dist folder.
What i'm trying to say, is that, should i compile my code to translate it into pure javascript in the dist folder in order to deplot, or is it okay just to just run the server in the server.ts file ?
This is how my project looks like
TypeScript is ultimately just there to help you structure your code properly, as it is meant to be compiled to JavaScript. While you can run your server via ts-node on a VM, it is simpler to just compile it to JS and run it on your VM, as at that point it is no different from a typical NodeJS application. Other advantages are smaller build files since your js files will take up less space and startup time.
That being said there is no stopping you from using ts-node on heroku by putting ts-node index.ts inside your package.json start script, (also ts-node should be in dependencies not devDependencies), but personally I'd go down the compilation route as that is pretty much just another NodeJS application.
See Heroku can't find ts-node

How to use NPM Modules with Flask?

I have a python application which I am deploying through Flask using the render_template() function onto a webpage. At the same time, I am trying to use npm to incorporate some javascript modules into my code. Whilst I have correctly installed the needed modules within the static folder with all my other javascript files, the code refuses to recognize these modules.
My Flask CLI shows that my local development server has correctly located the module file but if I run var module = require('module') the code shows no indication of having worked if run through the browser. This goes for whether I include this script inside my html template in the template folder, or an external javascript file in the static folder.
Interestingly enough, if I run the same external javascript file through the npm CLI using node script.js, the script will execute. Can someone explain what I'm doing wrong and why this is so? I'm completely new to node.js, npm and have just started today so any help would be appreciated.
I am currently basing my work off of the answer with 6 upvotes here: How can I serve NPM packages using Flask?
You can use electron as the ui for the python app by spawning your file and navigating to the local url in the app instead of using a browser. With this you will have some node capability.
Without knowing more, this is a bit of a stab in the dark, and quite late, however, I solved a similar problem with the following:
app = Flask(__name__,
static_folder = './public',
template_folder="./static")
npm is. Node.js packages manager tool. And it is only used node.js application.
If your application frontend is react or vue framework and your backend is node.js framework example Express or Koa, use npm is good. but now your backend is Flask
, you know Python package manage tools is Pip, so if you use Npm, you should use node in frontend , backend is flask, and frontend start npm start, backend start python app.py.

How to change webpack-dev-server with other server like express or apache

I have used vue-cli to scaffolding an vue app and now I want to setup a different web server rather then webpack-dev-server because I want a separate configuration file where I can configure the server like node express which I can use for production deployment.
Please suggest is there any way to configure the new server. Thanks
For development you should continue using webpack dev server (which is express).
For deployment you can use anything you want, just run:
npm run build
This will generate bundled javascript files, which you can then deploy anywhere.

Is it possible to pass node process variable to webpack builded package?

I am currently developing nodejs app which will be hosted on iis, using iisnode.
I am having such a problem, this is part of my server.js:
app.listen(process.env.PORT);
process.env.PORT is passed by IIS, so at the moment when I locally building webpack package I have no port.
I deploy the build-ed package on my server, but looking at server.bundle.js, I can see:
app.listen(undefined);
now IIS can not start the application..
Is there any option to set webpack bundle-ed package to expect node process variable to be passed on the run?
Or maybe I am doing something wrong here?
This sounds like a job for the DefinePlugin

Maven Profile concept in a NodeJS Project without MVN

Hello stakOverFlowers :D
I have a simple NodeJS WebApp that use Lerna to manage the project. So i have a package directory that contains n different projects each ones using different tasks runner tools.
I always use Maven Build Profile in java environment but for this NodeJS project maven will not be used.
So the question is...
Is there a way to reproduce the Maven Build Profile concept without using MVN?
In a nutshell i need to use a build profile in nodejs, without using MVN, to customize build for different environments such as Production v/s Development environments.
There's a way to do that?
thanks to all
you can do it by storing your configurations in a JSON file as key value pairs in the same way as you do in properties file in Java.
Then by someway or other invoke properties from the environment specific configuration file such as production.json or stage.json or qa.json.
One of the easy ways to do this is using this module named config
Using this you can pass NODE_ENV=production(or dev or qa whatever) and access relevant configurations. This will help you achieve environment profiling.
You can also store configurations in a JS file but I personally prefer a JSON file for storing configurations.
But if you're wondering for dependencies management that is done by package.json file which is somewhat similar to your pom.xml file.
For more details about it you might want to read this official documentation for it.
My solution, following the TGW advice, works!!
Just install the config module, and create a directory that containing .json files.
$ npm install config
$ mkdir config
$ vi config/default.json
Than if u are on a windows machine, choose your NODE_ENV using NODE_ENV=production and than execute your web app.
In your .js file put informations like yours dbConnection host and password.... and to retrieve it use:
var config = require('config');
var dbConfig = config.get('JsonItem.dbConfig');
..more details on https://github.com/lorenwest/node-config

Categories