How to serve static content in a Node Express container with Docker? - javascript

I used express generator to create a project, which automatically includes the public folder in app.js ...
I am trying to display an image in my index.ejs but it is not working ...
what would be the correct url to use?
After looking online it says that the src should be this img src="localhost:8000/images/1.png"
However, I am using docker toolbox, so I'm using the docker quick start terminal and I got it to work by using the docker i.p
so its like img src="127.432.343:8000/images/1.png"
but that IP is specific to my computer I want my co-workers to be able to see the image without having to refactor the code ...
the name of my container is web so I also tried I tried src="web:8000/images/1.png"
But this doesn't work, I feel like it should though, any tips please been trying for hours here.
Also if this were to go to production on a server, what would be the best way to do that? I don't want to have to change the code if I end up uploading it to AWS ...

Any chance you can switch to Docker for Mac? That would allow you to see it on localhost

Static content in express must be in the /public folder. Images under public/images folder. If your image is in that folder, then it should be accessible from the url you're saying.
To show it from an ejs file, you need to only point to images/1.png (use a relative route):
<img src="images/i.png" />
This way, you won't have any trouble uploading it to any production server.
For other computers in your network to test your project, you need to check your computer's local network ip (ipconfig / ifconfig) and test with the ip address port 8000. Not every internal network has it's DNS fully configured.
Also check your computer's firewall to allow tcp traffic through port 8000. Hope it helps.

Related

Excel Javascript Add-in Deployment On Local Network

I have developed an Excel Javascript Add-in on my machine using the yeoman generator. The add-in itself works fine when I start it up on my client with npm start. I can use the add-in on any port I want provided I change all the ports in the config files etc. However, we now want to delpoy this add-in on our local server to make it available to several users.
I have noticed that every time I change the location of the project e.g. from my machine to the server the application will start up but Excel will give me the error message "ADD-IN ERROR Sorry, we can't load the add-in. Please make sure you have network and/or Internet connectivity. Click "Retry" once you are back online". My work around for now is to create a new app on the server and copy the html, js and css file to the taskpane folder. When I now start up the new app it'll work fine.
For the second issue I haven't found a solution/understood the behaviour. When I create an app with the yeoman generator on our server (MS server 2019) and start it up it can be reached via e.g. localhost:3000 on the server but not via :3000 or :3000. It runs fine when I access it from the server itself.
I have opened the inbound and outbound port 3000 on the server to allow https communication. In a next step I added the manifest to the windows 365 admin console and added myself as user. When I open Excel from my machine (client side) the excel add-in will be available in the insert ribbon but won't load and I get the same error message as described above "ADD-IN ERROR Sorry, we can't load the add-in. Please make sure you have network and/or Internet connectivity. Click "Retry" once you are back online"
The following troubleshooting steps have been performed. I checked whether I can reach the add-in url via web browser but the connection timed out. When I check on the server side whether the app is listening I get the below result.
TCP 127.0.0.1:3000 0.0.0.0 Listening 40776
I'm not sure but I think the app should list on e.g. 0.0.0.0:3000 to capture inbound traffic from the client side. Is there a way to change this add-in (app) behaviour on the server when creating/starting up the app with the yeoman generator? I have checked the webpack.config.js file but I couldn't find any way to change that behaviour.
Any idea/help is appreciated.
Thanks!
I believe this is precisely the intended configuration of the yeoman development environment. It spins up the webpack dev (NodeJS) server, which by default only listens to the localhost / loopback address.
From here:
https://webpack.js.org/configuration/dev-server/#devserverhost
It looks like you could hand modify your webpack.config.js file to include a
host: '0.0.0.0' entry under the devServer listing.
The other item you might need to change is
https://webpack.js.org/configuration/dev-server/#devserverallowedhosts
As I believe this defaults to 'auto' which would only allow connections from the local computer.

CORS policy error on file in same folder as HTML [duplicate]

I am getting the following error:
XMLHttpRequest cannot load file:///C:/Users/richa.agiwal/Desktop/get/rm_Library/templates/template_viewSettings.html. Cross origin requests are only supported for HTTP.
I realize that this question has been answered before, but I still have not found a solution to my problem. I tried running chrome.exe --allow-file-access-from-files from the command prompt, and moved the file to the local file system, but I still get the same error.
I appreciate any suggestions!
If you are doing something like writing HTML and Javascript in a code editor on your personal computer, and testing the output in your browser, you will probably get error messages about Cross Origin Requests. Your browser will render HTML and run Javascript, jQuery, angularJs in your browser without needing a server set up. But many web browsers are programed to watch for cross site attacks, and will block requests. You don't want just anyone being able to read your hard drive from your web browser. You can create a fully functioning web page using Notepad++ that will run Javascript, and frameworks like jQuery and angularJs; and test everything just by using the Notepad++ menu item, RUN, LAUNCH IN FIREFOX. That's a nice, easy way to start creating a web page, but when you start creating anything more than layout, css and simple page navigation, you need a local server set up on your machine.
Here are some options that I use.
Test your web page locally on Firefox, then deploy to your host.
or: Run a local server
Test on Firefox, Deploy to Host
Firefox currently allows Cross Origin Requests from files served from your hard drive
Your web hosting site will allow requests to files in folders as configured by the manifest file
Run a Local Server
Run a server on your computer, like Apache or Python
Python isn't a server, but it will run a simple server
Run a Local Server with Python
Get your IP address:
On Windows: Open up the 'Command Prompt'. All Programs, Accessories, Command Prompt
I always run the Command Prompt as Administrator. Right click the Command Prompt menu item and look for Run As Administrator
Type the command: ipconfig and hit Enter.
Look for: IPv4 Address . . . . . . . . 12.123.123.00
There are websites that will also display your IP address
If you don't have Python, download and install it.
Using the 'Command Prompt' you must go to the folder where the files are that you want to serve as a webpage.
If you need to get back to the C:\ Root directory - type cd/
type cd Drive:\Folder\Folder\etc to get to the folder where your .Html file is (or php, etc)
Check the path. type: path at the command prompt. You must see the path to the folder where python is located. For example, if python is in C:\Python27, then you must see that address in the paths that are listed.
If the path to the Python directory is not in the path, you must set the path. type: help path and hit Enter. You will see help for path.
Type something like: path c:\python27 %path%
%path% keeps all your current paths. You don't want to wipe out all your current paths, just add a new path.
Create the new path FROM the folder where you want to serve the files.
Start the Python Server: Type: python -m SimpleHTTPServer port Where 'port' is the number of the port you want, for example python -m SimpleHTTPServer 1337
If you leave the port empty, it defaults to port 8000
If the Python server starts successfully, you will see a msg.
Run You Web Application Locally
Open a browser
In the address line type: http://your IP address:port
http://xxx.xxx.x.x:1337 or http://xx.xxx.xxx.xx:8000 for the default
If the server is working, you will see a list of your files in the browser
Click the file you want to serve, and it should display.
More advanced solutions
Install a code editor, web server, and other services that are integrated.
You can install Apache, PHP, Python, SQL, Debuggers etc. all separately on your machine, and then spend lots of time trying to figure out how to make them all work together, or look for a solution that combines all those things.
I like using XAMPP with NetBeans IDE. You can also install WAMP which provides a User Interface for managing and integrating Apache and other services.
Simple Solution
If you are working with pure html/js/css files.
Install this small server(link) app in chrome. Open the app and point the file location to your project directory.
Goto the url shown in the app.
Edit: Smarter solution using Gulp
Step 1: To install Gulp. Run following command in your terminal.
npm install gulp-cli -g
npm install gulp -D
Step 2: Inside your project directory create a file named gulpfile.js. Copy the following content inside it.
var gulp = require('gulp');
var bs = require('browser-sync').create();
gulp.task('serve', [], () => {
bs.init({
server: {
baseDir: "./",
},
port: 5000,
reloadOnRestart: true,
browser: "google chrome"
});
gulp.watch('./**/*', ['', bs.reload]);
});
Step 3: Install browser sync gulp plugin. Inside the same directory where gulpfile.js is present, run the following command
npm install browser-sync gulp --save-dev
Step 4: Start the server. Inside the same directory where gulpfile.js is present, run the following command
gulp serve
To add to Alan Wells's elaborate answer here is a quick fix
Run a Local Server
you can serve any folder in your computer with Serve
First, navigate using the command line into the folder you'd like to serve.
Then
npx i -g serve
serve
or if you'd like to test Serve without downloading it
npx serve
and that's it! You can view your files at http://localhost:5000
If you are using vscode, you can easily start a liver server. Click liver server at the bottom of the page, once the server is started, vscode will tell the port the project is running. Do ensure your project folder is the workspace
This error is happening because you are just opening html documents directly from the browser. To fix this you will need to serve your code from a webserver and access it on localhost. If you have Apache setup, use it to serve your files. Some IDE's have built in web servers, like JetBrains IDE's, Eclipse...
If you have Node.Js setup then you can use http-server. Just run npm install http-server -g and you will be able to use it in terminal like http-server C:\location\to\app.
Kirill Fuchs
If you use the WebStorm Javascript IDE, you can just open your project from WebStorm in your browser. WebStorm will automatically start a server and you won't get any of these errors anymore, because you are now accessing the files with the allowed/supported protocols (HTTP).
I was facing this error while I deployed my Web API project locally and I was calling API project only with this URL given below:
localhost//myAPIProject
Since the error message says it is not http:// then I changed the URL and put a prefix http as given below and the error was gone.
http://localhost//myAPIProject
Depends on your needs, but there is also a quick way to temporarily check your (dummy) JSON by saving your JSON on http://myjson.com. Copy the api link and paste that into your javascript code. Viola! When you want to deploy the codes, you must not forget to change that url in your codes!

Tomcat 404 error, image js and css are not found

I used tomcat 7 for deploy my war file. It contains various images, css and js file.
I configure my tomcat on port 8080. Everything goes fine but I don't know why these content not serve by tomcat. It gives 404 not found error.
Directory Structure
And I hit this below url
http://localhost:8080/images/jackson2.png
When I put these images outside ROOT folder to inside webapps/examples/ then it works fine.
Now what is wrong with it ? It there anything I missed ?
I could not understand the issue ? Help me
Perhaps check that tomcat and/or the image files have the correct permissions set, I'm not too sure how to go about it on windows, depending on which version your running it could be a UAC issue, you can try looking here: https://www.mulesoft.com/tcat/tomcat-windows. You can try stopping tomcat, remove the work and temp directories, then starting tomcat.

Simplest Way To Deploy Javascript Application On Linux

I have an folder say 'mywebapp' on windows machines. This folder has index.html page, js directory with java script files and css directory with css files.
Now when i open this index.html into browser, the browser displays contents pretty well, as if i have deployed this application on server, which is not the case.
Now i wanted to do same on my Linux machine vm, login-ed through putty. I tried using pythons SimpleHTTPServer which gave me same result. But as soon as i exit from putty session, the webpage doesnt display. seems like SimpleHTTPServer server connection is broken once i exit the putty session.
Please help me.
Or any other professional and easy way to get my webpage displayed. Tomcat seems good option but i don't have root permission and don't want hectic deployment process.
I heard about node.js, but i don't have root permission to install node.
Most simplest way i can suggest it to download and copy tar.gz file from location:
https://tomcat.apache.org/download-70.cgi
1 then gunzip and untar this downloaded file.
2 Go to conf/Catelina/localhost folder.
3 create an xml with your application name, e.g. mywebapp.xml
and put following to this file:
<Context path="/mywebapp" reloadable="false" docBase="<root-path of your application folder>"/>
here "root-path of your application folder" will be the root folder of ypur HTML, js and css files.
Then just start this tomcat using /bin/startup.sh command and check on browser using localhost:8080/mywebapp

socket.io.js not loading

I know there are a bunch of questions on this already, but none have answered it for me, and plus mine is slightly different.
I'm starting the socket.io server in node using this:
var io = require('socket.io').listen(8000);
My terminal says everything is ok:
info - socket.io started
Now I am trying to load the .js file in my clientside browser using this url:
http://<hostname>:8000/socket.io/socket.io.js
I dont get a 404, it just hangs forever. I've also used a network utility to ping port 8000, and it seems to be open fine.
I installed node and socket.io just yesterday, so they should be the latest versions. Can anyone shed any light on this? Thanks!
Turns out the reason I could never request the .js file was because my company network blocks all ports except the usual ones (80, 21, etc), so `I would never be able to communicate with port 8000.
Use express.js. Place the socket.io file in public/javascripts folder and add this line to your html
<script src="/javascripts/socket.io.js"></script>
I think this is the best way. When you're writing http://<hostname>:8000/socket.io/socket.io.js
node tries to find a folder named socket.io in your project's public folder. And the file socket.io.js in it.
If you don't want to use express.js you should catch the request and try to load a file if no routes were found for your request (what actually express does) because node doesn't know what to do for requests which don't match any routes in your server.
And I recommend to use the socket.io.min.js file (it's smaller and it's in folder node_modules\socket.io\node_modules\socket.io-client\dist)
You have to start an http/https server to access it via http/https. Simply starting an socket.io server won't do. Do the following:
var http = require('http');
var app = http.createServer(),
io = require('socket.io').listen(app);
app.listen(7000, "0.0.0.0");
Then I can access the file http://localhost:7000/socket.io/socket.io.js
sockets.io uses websocket protocol (ws://). See the wikipedia page.
You need to get at least 3 pieces working together.
Serve some HTML (/index.html will do just fine) so there's a web page. This file should contain the socket.io client <script> tag. For this you need the http server portion of the starter examples. You are missing this and that's why browsing to your server just hangs.
Serve the socket.io client. Socket.io will do this for you automatically when you pass in your http server function to it. You don't need full express as this can be done with just node's http module as per the first example on the socket.io docs.
Some javascript to actually do something with the socket. This can be the content of a <script> tag in index.html or a separate file. If it's a separate file, you need to set up your http server to actually serve it.

Categories