I have a flask app in which I have a front end html with multiple features.
This front end html template has a "download button" to download a file.
This is how I am triggering a download:
$('#download_excel').attr('onClick',"window.open('/static/excel.xlsx')");
download_excel corresponds to a bottom to download a file. This works fine.
But, if I move my excel.xlsx file to a different folder say "download" and modify the above line to this:
$('#download_excel').attr('onClick',"window.open('/download/excel.xlsx')");
This show no file here: (http://127.1.1.1:5555/download/excel.xlsx)
Why? what is the difference?
The problem is that Flask only serves files from the static folder. You have to specify another static folder path as described here: https://stackoverflow.com/a/9519004/5868802
This is because Flask has a default endpoint for static files (files accessible publicly) at /static. This endpoints binds to directory static in your project folder.
You can rebind this endpoint in Flask constructor:
Flask(__name__, static_url_path="/download", static_folder='download')
Related
how to load html file along with css file and javascrip file in node js by express?
when I enter localhost:8080\about, load the about us page that contain html,css and js.
for that in your main express js file, you are going to want to add this line at the top of the file
app.use(express.static(__dirname + '/pathToStaticFiles'));
pathToStaticFiles will be the path to your static file, (usually called assets or public). Then all your html files will read from that directory so if your file path for the css file is ./static/css/style.css the html file will call from ./css/style.css. Hope this helped.
I just want to ask how to use the jquery custom builder since i separate the folder of jquery custom builder to the Login Folder. Here is the folder path for the jquery custom builder
And here is for the Login Folder
I have tried this kind of syntax for getting the Directory of the js file and to other files to but it doesn't seems to work.
<script src = "../htdocs/WebSite/jslib/jquery-ui-1.11.4.custom/jquery-ui.js"></script>
i hope you can help me with this since i'm just starting jquery i also read the guide for using jquery i follow the instruction but it's still the same.
Thanks
Where is the html file that imports jquery script tag? It seems like just path problem. Usually, URI paths are based on app server root. There are so many ways managing URI, but XAMPP might let file resource paths show up same as URI paths.
When app server root is located on c:/foo/bar/:
c:/foo/bar/lib/jquery.js -> http://localhost:xxxx/lib/jquery.js
c:/foo/bar/index.html -> http://localhost:xxxx/index.html
So in index.html, import resource as this way.
<script src="lib/jquery.js"></script>
I'm looking at this sample Angular application.
In the index.html file, there are lines like
<script type="text/javascript" src="/static/angular.js"></script>
However, upon closer inspection there are no folders in the project called static.
How does this work? How does angular locate these references?
Angular has nothing to do with this. It is the express server which takes care of the paths.
Take a look at server/config.js. You will see staticUrl: '/static' mentioned there.
Now open server/server.js (server.js is the script which runs before anything else runs in the app so all the configuration is done within this file) and on line 21 you will see require('./lib/routes/static').addRoutes(app, config);. This requires the static.js file which instructs the app to use /static (mentioned in the config.js file) as the path for static files such as CSS and javascript files.
This is a server side phenomenon. There is a middleware in this file server/lib/routes/static.js :
line : 9
app.use(config.server.staticUrl, express.static(config.server.distFolder));
What this does is : if any http request is started from config.server.staticUrl (whitch is /static for this app) the server tries to respond with the resource that are kept in a config.server.distFolder folder (which is client/dist for this app).
For example :
when you request to this url /static/angular.js the middleware tries to find angular.js file in client/dist/. Because client/dist directory is mapped to the url which starts with /static by the middleware.
When that HTML file is processed by the browser, the layout engine is making a separate HTTP request to the server to download the resource in question:
/static/angular.js
Since all of that is handled by the server routing mechanism there doesn't have to be a folder named static in client code. Your example is using Node.js Express routing which maps /static routes to actual physical paths.
Here is a piece of code that configures static routes:
https://github.com/angular-app/angular-app/blob/master/server/config.js
The important parts are:
staticUrl: '/static', // The base url from which we serve static files (such as js, css and images)
And the destination folder that /static maps to:
distFolder: path.resolve(__dirname, '../client/dist'), // The folder that contains the application files (note that the files are in a different repository) - relative to this file
Per the documentation the dist folder contains the Grunt build results, and if you take a look at the gruntfile you will find all the grunt configuration that makes this possible.
https://github.com/angular-app/angular-app/blob/master/client/gruntFile.js
I'm trying to convert an Express app templated with EJS to Angular. When trying to use the Angular Strap popover I want the popover content to show the data from a HTML partial. The problem is when I reference the file the server makes a GET request for that route and cannot be found.
My express configuration has the index.html file in a templates folder. The statics are in a public folder in a neighboring directory. The complete folder structure looks like this:
app
--public
--templates
-index.html
-user.html
The popover content I want is in user.html and it's being referenced from index.html. My angular code looks like this:
<a
title="User Info"
data-content="test"
data-template="/user.html"
bs-popover>whatever</a>
I've tried user.html, /user.html, and any combination of those. In the console it throws a 404 error from the root saying that http://hostname.com/user.html doesn't exist. My question is, how do I reference a partial html file in angular and get it to point to the right file and not to a GET request to the server?
I've also tried using angular-ui-bootstrap but it lacks partials support.
Thanks!
The files need to be in the folder defined by your Express configuration as the statics folder. So /user.html is looking in public/user.html for the file. If it doesn't find it, then it sends a GET request to the server.
I have a node.js file server running which (when visited) returns a html content. Also in the same directory of the node.js file, there is a javascript file called test.js. Also in the html content being returned, I need to load that javascript file. However in the html content, being returned, which comes out to be called index.html, the script looks like
<script type="text/javascript" src="test.js"></script>
But the server isn't sending the test.js file, its only sending the index.html file, and the script link is loading it relatively from the same directory.
Now I don't want to give the url to the test.js file. I want the node.js file to also send the test.js file, so that it ends up in the same directory as index.html. And then index.html can load it from the same directory.
Is there a way I can specify in the node.js with code to also send the test.js file?
Thanks.
Are you familiar with Express, as dandavis mentioned? Express allows you to set a directory for your static files. See my standard config below:
app
.use('view engine', jade)
.use(express.compress())
.use(express.limit('10mb'))
.use(express.bodyParser())
.use(app.router)
.use(stylus.middleware({
src: __dirname + '/www',
compile: function(str, path) {
return stylus(str)
.set('filename', path)
.set('compress', false)
.set('warn', true);
}
}))
.use(express.static(__dirname + '/www'))
.use(express.logger());
The important part here is second from the bottom. Essentially, Express now knows to look for any assets you specify in your HTML within that static directory. For me, I create a folder titled WWW within my server folder, then add to it my JS, CSS, and images.
For example, say I create the "stylus" folder within my WWW folder, and add to it store.css. My link to that CSS asset would be the following in my Jade template:
link(rel="stylesheet", type="text/css", href="stylus/store.css")
Express knows to look for that asset relative to the __dirname + '/www' path, and thus locates the "stylus" folder and the CSS assets it contains. Hope this helps, and that I haven't ventured away from your intent!