I have included an existing project file in my karma.conf.js files array:
files : [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-route/angular-route.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/js/**/*.js',
'test/unit/**/*.js',
'bower.json',
'app/data/file.json',
],
I know the file has been matched because I do not get a warning that
WARN [watcher]: Pattern <pattern> does not match any file. I changed that line to a known non-existent file and back to double check.
My base path is the project root:
basePath : '../',
When Angular's controllersSpec.js runs, I use an XHR GET to synchronously load the file, but I get a HTTP 404. Where is the file?
//Synchronously GET the test data
var req = new XMLHttpRequest();
req.open('GET', 'app/data/file.json', false);
req.send(null);
var testData = JSON.parse(req.responseText);
Then, in the test shell, I see:
WARN [web-server]: 404: /app/data/file.json
<user agent> ERROR
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
at <path to project>/test/unit/controllersSpec.js:12
It looks to me like you're trying to load a local file with a GET request; depending on what port or host your server is running on, it may not be able to load it up via a relative path. Try instead pointing it to the full URL path including protocol and domain name, e.g.:
//Synchronously GET the test data
var req = new XMLHttpRequest();
req.open('GET', 'http://servername.com/app/data/file.json', false);
req.send(null);
var testData = JSON.parse(req.responseText);
Related
How to use the angular-dynamic-locale with webpack?
The angular-dynamic-locale always tries to load the angular-locale_en.js file from path http://localhost:8080/angular/i18n/angular-locale_de.js during the running-time, when the "tmhDynamicLocale.set('de');" is performed.
I'm using webpack therefore I define every dependencies either in the top of my app.js or in the top of my controllers. I tried to define this with require('angular-i18n/angular-locale_de') or with import, but unfortunatelly, I always get the following error messages:
GET http://localhost:8080/angular/i18n/angular-locale_de.js net::ERR_ABORTED 404 (Not Found)
Refused to execute script from 'http://localhost:8080/angular/i18n/angular-locale_de.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
If you use your locales like this:
tmhDynamicLocaleProvider
.localeLocationPattern('./angular/i18n/angular-locale_{{locale}}.js')
.defaultLocale('de');
You can probably use CopyWebpackPlugin like this:
new CopyWebpackPlugin([
{from: './node_modules/angular-i18n/angular-locale_de.js', to: path.resolve(__dirname, '.[WEBPACK OUTPUT FOLDER]' + '/angular/i18n')}
])
Be sure that the destination folder matches the output of your webpacked files
I am trying to pull in the current URL to use in my fetch command, but I received an error when attempting to parse the URL via window.location. It appears that this URL use is not allowed, but I'm not sure of another alternative to achieve what I'm looking for. I'm avoiding setting a string so I can adapt my React setup to multiple environments.
Error:
Error: only absolute urls are supported
Line creating the error:
console.log(protocol + '//' + hostname + ':' + port + '/api' + window.location.search);
Variables:
var urlPath = window.location.pathname;
var hostname = window.location.hostname;
var protocol = window.location.protocol;
var port = window.location.port;
I prefer dotenv. Then all you need to do is to create a .env file. There are a couple ways of loading it into your app.
Webpack(not the npm package listed above, instead is webpack-dotenv-plugin)
import DotenvPlugin from 'webpack-dotenv-plugin';
plugins: [
new DotenvPlugin({
sample: './.env.default',
path: './.env',
}),
Not webpack
As early as possible:
require('dotenv').config()
my Webpack version is v1.15.0.
I just require('fs') in my JS file, but I got the error:
Uncaught Error: Cannot find module "fs";
and when I add node{ fs: 'empty' },
and then I got another error:
fs.readdirSync is not a function
if I add:
externals:{
"fs": "commonjs fs"
},
I get another error:
Uncaught ReferenceError: require is not defined
Why? How can I fixed it?
EDIT : when I put all my file's names on a file .txt and I get the file .txt with $http.get it's working fine
fs should be server side only (nodeJS) you would always make node do the work of accessing files on the server, not the client side.
You would do something like this, on NodeJS (server side) (this isnt word for word correct, but it'll get you started)
fs = require('fs')
router.get('/getdocs', function(req, res, next) {
// do your filesystem operation here, then return what you want
})
Then on the client side (angularJS) you would fetch what NodeJS returns
fetch('mydomain/api/getdocs').then(rtrn => {
console.log(rtrn)
})
It is always recommended to have the inbuilt or the function which arent gonna change to use it with constant
const fs = require('fs');
having it constant throws a error when you try to modify it.
consider the following scenario:
My express server dynamically generates HTML for the "/" route of my single page application.
I would like to re-serve this same generated HTML as the service worker navigateFallback when the user is offline.
I'm using https://www.npmjs.com/package/sw-precache-webpack-plugin in my webpack configuration.
If I generate an index.html via html-webpack-plugin, say, and set index.html as my navigateFallback file, that generated file gets served correctly by the service worker.
However, I can see no way to cause the on-the-fly rendered index html (what the live server returns for the "/" path) to be cached and used as the offline html.
Use dynamicUrlToDependencies option of Service Worker Precache to cache your route url and its dependencies. Then set navigateFallback to '/' and navigateFallbackWhitelist to a regex matching your sublinks logic.
Take this configuration : (Add const glob = require('glob') atop of your webpack config)
new SWPrecacheWebpackPlugin({
cacheId: 'my-project',
filename: 'offline.js',
maximumFileSizeToCacheInBytes: 4194304,
dynamicUrlToDependencies: {
'/': [
...glob.sync(`[name].js`),
...glob.sync(`[name].css`)
]
},
navigateFallback: '/',
navigateFallbackWhitelist: [/^\/page\//],
staticFileGlobsIgnorePatterns: [/\.map$/],
minify: false, //set to "true" when going on production
runtimeCaching: [{
urlPattern: /^http:\/\/localhost:2000\/api/,
// Use network first and cache as a fallback
handler: 'networkFirst'
}],
})
That use case should be supported. I have an example of something similar using the underlying sw-precache library, and I believe the syntax should be equivalent when using the Webpack wrapper.
In this case, /shell is the URL used for dynamically generated content from the server, constituting the App Shell, but it sounds like your use case is similar, with / instead of /shell.
{
// Define the dependencies for the server-rendered /shell URL,
// so that it's kept up to date.
dynamicUrlToDependencies: {
'/shell': [
...glob.sync(`${BUILD_DIR}/rev/js/**/*.js`),
...glob.sync(`${BUILD_DIR}/rev/styles/all*.css`),
`${SRC_DIR}/views/index.handlebars`
]
},
// Brute force server worker routing:
// Tell the service worker to use /shell for all navigations.
// E.g. A request for /guides/12345 will be fulfilled with /shell
navigateFallback: '/shell',
// Other config goes here...
}
I have an angularJs app that sends a base64 encoded image (or file) to my rails4 server api that uses paperclip to store attachments. Everything works fine until the content_type_validation paperclip does.
For some reason, paperclip determines the content-type's been spoofed and get the following error message:
[paperclip] Content Type Spoof: Filename 1413325092.jpg (["image/jpeg"]), content type discovered from file command: application/octet-stream; charset=binary. See documentation to allow this combination.
I create the paperclip attachment with the following code:
def self.create_from_base64(base64_string)
decoded_data = Base64.decode64(base64_string)
# create 'file' understandable by Paperclip
data = StringIO.new(decoded_data)
data.class_eval do
attr_accessor :content_type, :original_filename
end
# set file properties
data.content_type = 'application/octet-stream'
data.original_filename = "#{Time.now.to_i}.jpg"
end
I've tried different things but for some reason even when I set data.content_type = 'application/octet-stream', the error is exactly the same, and paperclip it's been spoofed.
Any ideas?
Thanks,
EDIT:
I have the following validation:
validates_attachment_content_type :file, :content_type => [/png\Z/, /jpe?g\Z/, /application\/octet-stream*/]