Jade: dynamic image url - node.js

I'm trying to set an image url in jade...
I have this: img(src = 'http://192.168.1.8:8081')
I need to change 192.168.1.8 automatically with the server address...
For example if I connect to my server from office, my url should become img(src = 'http://myPUBLICserveraddress:8081')
How can I do this?
Thanks

I do this with Dust.js, but the principle should be the same. The way I do this is to set a hostname and port attribute on the app for both development and production (which is assigned in app.configure('development') and app.configure('production')), and then in the templates, I just do the Dust.js equivalent of:
- if (port)
img(src="http://#{host}:#{port}")
- else
img(src="http://#{host}")
And I get what I'm looking for, which is the right link based on the environment (dev vs production).

Related

how can i get dynamic url in nodejs? (mernstack)

hi im trying to make forgot password/reset password service.
let me show you the logic of this service.
(simply, send email in react(localhost:3000/forgotPassword) -> click link in email -> route to localhost:3000/resetPassword)
1.send email in frontend
The content is to write down the email of the account you joined.
2.email
비밀번호 리셋하기 <- this is direct to http://localhost:3000/resetPassword
3.forgotPassword.js in node.js
Here, I want to change resetURL(http://localhost:3000/resetPassword) to dynamic URL. Because I will distribute this soon.
3.package.json in react
I was able to do api communication from react to node by specifying proxy here, but /api/resetPassword does not work in node.
To sum up, I want to use the code used by React on the node as well. Like this.
You are looking to environmentalise your configuration.
You could do it through an environment variable, like this:
const baseURL = process.env.BASE_URL || 'http://localhost:3000'
const resetURL =`${baseURL}/resetPassword`
If you use the dotenv package, you can support configuration through environment variables or from a .env file.
If I understand correctly, your interested in setting up a dynamic variable for the url in your react app. Try just setting an environment config file, either a file for each environment you are developing in (i.e., Dev, QA, PROD) or a single file with multiple shared Env variables. You can even add an env variable to your package.json file for example.
NOTE the approach below would work for linux and mac, but not windows
please use the resources below for more information. This approach has been made available for react-scripts#0.9.0 and higher.
{
"scripts": "APP_URL=http://localhost:300 npm start"
}
Resources:
https://medium.com/#trekinbami/using-environment-variables-in-react-6b0a99d83cf5
https://create-react-app.dev/docs/adding-custom-environment-variables/

corrupt development.ini code

I am working through a python pyramid tutorial, and I have been making as many notes as I can inside the files I am writing.
something strange happened, I'd like to know why.
I wrote the development.ini file like was done in the tutorial then added notes.
# we are using this file for configureation in development
# config our wsgi
[app:main]
# which entry point to use as the app
use = egg:mysite
# reloads when templates are changed, not to be used in production
pyramid.reload_templates = true
#which server to use
[server:main]
#get the main entry point from the waitress package
use = egg:waitress#main
host = 0.0.0.0
port = 6534
# this is a great way to remove code for the rest of our package
# more importantly this file is easy to tweak for launching our package in a different manner
running pserve development.ini chrome returns:
This site can’t be reached
0.0.0.0 refused to connect.
Search Google for 6543
ERR_CONNECTION_REFUSED
I remove the comments:
[app:main]
use = egg:mysite
pyramid.reload_templates = true
[server:main]
use = egg:waitress#main
host = 0.0.0.0
port = 6534
I receive the same error as before.
This site can’t be reached
0.0.0.0 refused to connect.
Search Google for 6543
ERR_CONNECTION_REFUSED
THEN I copy and paste code from the tutorials repo into development.ini:
[app:main]
use = egg:mysite
pyramid.reload_templates = true
[server:main]
use = egg:waitress#main
host = 0.0.0.0
port = 6543
I am successfully able to reach localhost.
I am most interested to know, why this happened, how to avoid this problem, and if possible how to comment a development.ini file.
note:
I am using PyCharm as my ide
I am running this code on my local computer
ERR_CONNECTION_REFUSED means that the port you entered in Chrome's address bar does not align with the port number configuration in your .ini file. Look very carefully at your port numbers to make sure they align. You transposed the 4 and 3 in your original .ini (port = 6534), so I assume you tried to reach http://0.0.0.0:6543 in Chrome's address bar.
Bonus tip: PyCharm allows you to compare a single file's history on disk as well as version control. This helps reveal typographical errors. Right/CTRL-click on the file, Local History > Show History.

Changing Express constant based on URL (ie localhost vs production)

I'm fairly new to node and express...
I have a constant I set in my Node/Express application for "Domain". I'm constantly switching between localhost and the actual url when I switch between development and production.
I'm trying to figure out how I can automatically test the URL so that when the code is read on the server (ie actual domain url vs localhost) that it sets the proper domain url.
Here is my code:
function define(name, value) {
Object.defineProperty(exports, name, {
value: value,
enumerable: true
});
}
define("DOMAIN", "http://www.actual_domain.com");
// define("DOMAIN", "http://localhost:3000");
Anyone have a simple solution for this?
Thanks!!
there is many solutions for this, but usually this is done by environment variables, depends on your platform, but, in Linux systems, you do the following in your shell
export ENV_URL="http://www.example.com"
and to make sure its exported successfully
echo $ENV_URL
and in your code you do
const base_url = process.env.ENV_URL || "http://www.default.com";
in your local machine you set the ENV_URL to localhost or whatever you prefer, and in your server you set it to your actual URL.
or you could simply have many configuration files and you can determine the appropriate one by the environemnt variable like
export ENV=PROD
and in your code you can load the prod configuration file that contains your environment configurations.
The de facto way to do this sort of thing is through environment variables. Have a look at 12factor.net for a load of best practices, but in general you want to put differences in the environment into environment variables, then read those variables at runtime.

Serve out swagger-ui from nodejs/express project

I would like to use the swagger-ui dist 'as-is'...well almost as-is.
Pulled down the latest release from github (2.0.24) and stuck it in a folder in my app. I then server it out statically with express:
app.use('/swagger', express.static('./node_modules/swagger-ui/dist'));
That works as expected when I go to:
https://mydomain.com/swagger
However I want to populate the url field to my swagger json dynamically. IE I may deploy to different domains:
https://mydomain.com/api-docs
https://otherdomain.com/api-docs
And when I visit:
https://mydomain.com/swagger
https://otherdomain.com/swagger
I would like to dynamically set the url.
Is that possible?
Assuming the /api-docs (or swagger.json) are always on the same path, and only the domain changes, you can set the url parameter of the SwaggerUi object to "/path/to/api-docs" or "/path/to/swagger.json"instead of a full URL. That would make the UI load that path as relative to the domain the UI is hosted on.
For reference, I'm leaving the original answer as well, as it may prove useful in some cases.
You can use the url parameter to set the URL the UI should load.
That is, if you're hosting it under https://mydomain.com/swagger you can use https://mydomain.com/swagger?url=https://mydomain.com/api-docs and https://mydomain.com/swagger?https://otherdomain.com/api-docs to point at the different locations.
However, as far as I know, this functionality is only available at the current alpha version (which should be stable enough) and not with 2.0.24 that you use (though it's worth testing).
Another method would be to use the swagger-ui middleware located in the swagger-tool.
let swaggerUi = require('../node_modules/swagger-tools/middleware/swagger-ui');
app.use(swaggerUi(config.swagger));
The variable config.swagger contains the swagger.yaml or swagger.json. I have in my setting
let config = {
appRoot: __dirname,
swagger: require('./api/swagger/swagger.js')
};
Note: I am using the require('swagger-express-mw') module
You could try with this on index.html file of the swagger-ui... It works for me.
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = window.location.origin + "/path/to/swagger.json";
}

Breeze Entity manager relative path

A similar question is HERE but with no solution. I am able to run my project on localhost but once deployed, loading the metadata fails. Looking into chrome developper tools, the request is sent to http://www.domain.org/breeze/metadata instead of http://www.domain.org/projectname/breeze/metadata. On my localhost, the metadata loads as http://localhost:xxxx/breeze/Metadata. My entity manager is declared as new breeze.EntityManager('breeze'). If I change the entitymanager to new breeze.EntityManager('projectname/breeze') all works fine but it does not work on my localhost. How can I declare the entity manager so that the path is relative to the project and works on my localhost and when deployed?
It appears that you can assume that the data server and the web server share the same origin. Therefore, you can construct the origin (whatever that is) from the browser's window.location object (inject $location in Angular):
var origin = location.protocol+'//'+location.host+'/'; // 'ex: http://www.foo.com:3000/'
var projectName = location.hostname === 'localhost' ? '' : 'projectName/';
var serviceName = origin + projectName + 'breeze'.
var manager = new breeze.EntityManager(serviceName);
Alternatively, you could delegate the task to the web server which could construct the service name and plunk it in the web page as a JavaScript variable that you pick up during client-side configuration.
I'm sure you can take it from here if you need to make other assumptions.

Resources