I'm using bower to grab the .css and .js files for my website.
The directory structure looks like this:
server.py
bower_components/
templates/
All of the HTML templates (and some partials templates that I use with AngularJS) are located in templates. Stuff installed by bower is located in bower_components. How can I define template_path, static_path and static_url_prefix settings so that I can link into files in those directories?
If I use relative paths like:
href="bower_components/bootstrap/dist/css/bootstrap.css"
I will get 404 error. No handlers can be found so the error is thrown. It seems that Tornado forces me to use static_url function on my templates? Do I really have to use it? It looks very ugly when mixed with AngularJS. I have tried setting static_path to os.path.dirname(file) and tried using static_url but that gives me exception:
Exception: You must define the 'static_path' setting in your application to use static_url
How should I configure this? I can't believe I have wasted hours trying to figure this out already... :(
Did you try to use the StaticFileHandler? You can use something like this:
import os
import tornado.web
import tornado.ioloop
if __name__ == '__main__':
app = tornado.web.Application([
(r"/(.*)", tornado.web.StaticFileHandler, {"path": os.path.dirname(os.path.abspath(__file__))}),
])
app.listen(8888)
tornado.ioloop.IOLoop.instance().start()
You must combine staticfilehandler with some path variables you put in the settings variable.
http://tornado.readthedocs.org/en/latest/web.html
It's pretty easy. You can name as many settings variables as you want.
It turned out I needed to use tornado.web.StaticFileHandler class with path mapped to it. This is how I configured my tornado.web.Application:
class TornadoApp(tornado.web.Application):
def __init__(self):
# static file regex patterns matched with the actual folders here
static_handlers = {
'/bower_components/(.*)': 'bower_components',
'/templates/partials/(.*)': 'templates/partials'
}
handlers = [
('/', IndexHandler),
('/products', ProductHandler),
]
# append the handlers with content from static_handlers dictionary
for r, p in static_handlers.items():
handlers.append((r, tornado.web.StaticFileHandler, {'path': p}))
settings = {
'template_path': "templates",
'debug': True
}
tornado.web.Application.__init__(self, handlers, **settings)
Related
I just deployed a django website that I made. This is my first time of doing something like this.. still a newbie. The problem is that whenever I try to upload something to the website, I get the following error while DEBUG is True
Page not found (404)
Request Method: POST
Request URL: https://majestylink.com/admin/music/music/add/
Raised by: django.contrib.admin.options.add_view
Using the URLconf defined in majestylink.urls, Django tried these URL patterns, in this order:
[name='index']
advertise/ [name='advertise']
about-us/ [name='about-us']
promote/ [name='promote']
privacy-policy/ [name='privacy-policy']
s/ [name='search']
admin/
poem/
video/
music/ [name='index']
music/ <slug:slug>/ [name='detail']
ckeditor/
^media\/(?P<path>.*)$
The current path, music/music/add/, didn't match any of these.
The code below is my music app url..
from django.urls import path
from . import views
app_name = 'music'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<slug:slug>/', views.detail, name='detail'),
]
And this one is my project url..
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', include('home.urls')),
path('admin/', admin.site.urls),
path('poem/', include('poems.urls')),
path('video/', include('video.urls')),
path('music/', include('music.urls', namespace='music')),
path('ckeditor/', include('ckeditor_uploader.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
What else am I supposed to do in order to be able to publish something on the website? Am still new so I have a limited knowledge on how these things works
Everything works fine on local server, I get any error while testing it on local machine.
I made a slight change while deploying the website which is changing the database from the default sqlite3 to MySQL, will this be the cause? If yes, how can I resolve it. All migrations were run successfully
Your URL points to a admin/music/music/add path. I'm guessing you meant admin/music/add. You may want to share the template that triggers the add command.
I'm pretty new to Node.js and try to learn it for my work.
I want to import a class name "IgApiClient" from a file with name "client.ts"
the file exists in "core" folder which it exists in "src" folder.
the file I try to run is under "example" folder which it and "src" are in one folder.
Here is screenshots of what I said:
first
second
I think I should use the import statement for this as below:
import { IgApiClient } from '../src';
which is for nodejs version before 13 and for ver 13 and above I should use this as below:
const { IgApiClient } = require('../src');
I'm using the second one for my purpose.
But when I run the code it get me this error:
the error
I also tried to move the "client" file to "node_modules" folder and replace "../src" with "../src/core/client", but none of them worked.
what's wrong with it?
How can I solve it?
If anyone has a better idea for this I appreciate it.
Use babel to use ES6 feature like classes in NodeJs. You check this as an example babel-node.
Sometimes I see paths written like "#/assets/XXX", and I reckon it refers to the root maybe (in Nodejs)? But i guess it's a syntax that doesn't apply everywhere because when I want to refer to the root folder and try to use it, it sometimes break. I am not sure the implications of it.
The "#" is often used as an alias for a frequently used path (like src/) in webpack environments. You have to define it in your configuration file so the "#" can be resolved in the build-process.
If you work in an ES6 environment and import a component several times, it can be handy to create an alias for the component path.
Example (source: webpack documentation):
resolve.alias
object
Create aliases to import or require certain modules more easily. For example, to alias a bunch of commonly used src/ folders:
webpack.config.js
module.exports = {
//...
resolve: {
alias: {
Utilities: path.resolve(__dirname, 'src/utilities/'),
Templates: path.resolve(__dirname, 'src/templates/')
}
}
};
Now, instead of using relative paths when importing like so:
import Utility from '../../utilities/utility';
you can use the alias:
import Utility from 'Utilities/utility';
You can find a similar answer here: https://stackoverflow.com/a/42753045/10764912
I am building a React app using next.js, and I am playing around with the webpack config, in the next.config.js file.
Specifically, I'd like to have nicer imports using webpack's resolve.modules config.
However, when I add the following line in the next.config.js file :
config.resolve.modules
.concat(['styles','static','components','imports'])
and then
import FooBar from 'components/index/FooBar", for example in a pages/index.js file, it still won't work. FooBar is not found.
The component exists, and the import works fine if I use a relative path. However I'd like to have nicer imports, and I know it is possible with webpack (see react-boilerplate for example).
Am I doing something wrong with webpack ? Maybe it's a real bug ?
Check the NextJS example with-absolute-imports
const path = require('path')
module.exports = {
webpack (config, options) {
config.resolve.alias['components'] = path.join(__dirname, 'components')
return config
}
}
Alternatively, should work by adding this to next.config.js file:
config.resolve.modules.push(path.resolve('./'));
(and it doesn't require any babel plugin)
resolve.modules will look into the directories you configured for the modules you import. So when import components/index/FooBar it will look in:
styles/components/index/FooBar
static/components/index/FooBar
components/components/index/FooBar
imports/components/index/FooBar
A relative path looks further, but that's not relevant here and the path remains the same, just climbing up the directory tree (see resolve.modules).
Presumably none of these paths match your component. To get component/index/FooBar you need to import just index/FooBar.
import FooBar from 'index/FooBar';
Let's say i have following codes:
var mod1 = require('../../../../ok/mod1');
var mod2 = require('../../../info/mod2');
It's not pretty coding like above, i am wondering if there is a way to configure the root resolver just like webpack-resolve-root in nodejs?
So far as i know, the NODE_PATH can be used to replace the root of node_modules, but that's not what i want. I'd like to have the resolver to resolve multiple folders in order.
Updated answer for 2021.
nodejs subpath imports have been added in: v14.6.0, v12.19.0
This allows for you to add the following to package.json
"imports": {
"#ok/*": "./some-path/ok/*"
"#info/*": "./some-other-path/info/*"
},
and in your .js
import mod1 from '#ok/mod1';
import mod2 from '#info/mod2';
There is an npm package called module-alias that may do what you are looking for.
The best way to approach this would be to use a global (config) container.
In most cases you will have a config file in your application. In this config you can add a property which will be an object containing all absolute paths to files/folders.
Because config files are used at the start of you application, you just do the following:
var config = require("./config.js");
//config = {... , path: {"someModule": "/absolute/path/to", "someModule2": "/absolute/path/to"...}}
global.CONFIG_CONTAINER = config
Later on in your application you can just use
var myModule = require(CONFIG_CONTAINER.path.someModule)
// + concat if you are looking for a file
In case you have some complex paths and you need a more dynamic system, you can always implement a function inside the config that will build paths for you. ( config.makePath = function(){...} )
That should take care of it in a nutshell.