I am pretty new to CoffeeScript. I am trying to create Node.js application using the Express.js framework. And in my express app configuration I have this line of code that is compiling wrong:
app.use express.static path + '/public'
it is compiling to this:
app.use(express["static"](path + '/public'));
when I need to be this:
app.use(express.static(path + '/public'));
Does anyone know why this happening and how to fix this? It is causing my public folder to unaccessible.
I am using CoffeeScript 1.3.1
static could be a reserved word in future versions of javascript/ecmascript. Just like top now. So using it as a variable name could cause errors somewhere.
That's why coffee is trying to avoid it.
But they are equivalent, so try to find errors somewhere else.
They're equivalent, don't worry about it.
Express framework using 'serve-static' module for export static method:
exports.static = require('serve-static');
You may try solve your problem like this:
app.use '/static', require('serve-static')(__dirname + '/static')
or override static method in your module.
Related
I am building a project with Electron, and using Webpack to build the (Angular 2) render process app.
In this app, I need to dynamically require some files at run-time which do not exist at build-time. The code looks something like this:
require("fs").readdirSync(this.path).forEach(file => {
let myModule = require(path.join(this.path, file));
// do stuff with myModule
});
The problem is that the Webpack compiler will convert the require() call to its own __webpack_require__() and at run-time, it will look in its own internal module registry for the dynamic "myModule" file, and of course will not find it.
I have tried using the "externals" config option, but since this is a dynamic require, it doesn't seem to be processed by "externals".
Anyone else had success in solving this problem?
As suggested in a comment to my question by #jantimon, the solution is to use global.require:
require("fs").readdirSync(this.path).forEach(file => {
let myModule = global.require(path.join(this.path, file));
// do stuff with myModule
});
I came across this article and for some other reason the author needs node modules which gets not transpiled by webpack. He suggested to use
new webpack.IgnorePlugin(new RegExp("^(fs|ipc)$"))
in the webpack.config.js file. This should prevent to transpile the module fs and ipc so it can be used (required) in code.
I am not really sure if this hits also your problem but it might help.
The original article for more context can be found here: https://medium.com/#Agro/developing-desktop-applications-with-electron-and-react-40d117d97564#.927tyjq0y
I want to set a customisable layout path in Jade.
I get the path from my app and put it in Express in res.locals._layout like it (app.js is under /lib):
app.set('views', __dirname + '/../views');
app.set('view engine', 'jade');
res.locals._layout = layout_path;
Then I try to pass it to extends in my view like it:
extends _layout
I also tested extends #{_layout}, with also bad results...
Here is the error I get for the last one:
ENOENT, no such file or directory '/root_path/views/#{_layout}.jade'
The doc is not verbose on such a point.
I don't think what you're trying to do is supported by jade. The extends is resolved when the template is compiled, before any res.locals state can be applied. But there are some workarounds mentioned in this thread.
https://github.com/jadejs/jade/issues/520
I'd like to serve /mbti/:lang/ with static files from directory ./mbti/:lang/.
That is:
/mbti/en/ -> ./mbti/en/index.html
/mbti/en/some.json -> ./mbti/en/some.json
I found static, but seems like it can't do this:
app.use('/mbti/:lang/', express.static(__dirname + '/mbti/' + :lang));
How can I implement this? thanks.
You shouldn't need to do that, just make /mbti/ a static route and anything below that root will be served as static
express.static(__dirname + '/mbti');
I have the following code in Visual Studio, in an MVC application;
/scripts/bin/models/ViewModel.ts
export class ViewModel {
// view model code
}
Now, I have downloaded requirejs, and set the build mode for typescript to AMD type, so that its output looks such as....
define(["require", "exports"], function(require, exports) {
And so on ...
So then I declare my app/config.js file like so;
require.config({
baseUrl: '/scripts/bin'
});
And I try to load this up, I have requirejs loaded into the scripts, and attempt to call it...
require(['models/ViewModel'], function (viewModel) {
console.log("test");
});
And I am simply told that it is an invalid call. No other details. The path that it shows is completely correct, too. Is there some kind of additional configuration required? The requirejs documentation is extremely vague about this.
SOLUTION
This turned out to have nothing to do with requirejs, but instead had to do with IIS.
By default, IIS has a rule known as hiddenSegments. It does not allow you to bring in any code from a folder with bin in the path. I simply renamed the folder from bin to something else, and it worked fine.
Using require.js with TypeScript is a combination of your .html, require.config, module exports and imports.
For a step-by-step guide on moving from CommonJs TypeScript to AMD and require.js, have a look here.
Have fun.
The TypeScript compiler doesn't have any knowledge of your require.config - so when you use paths relative to that baseUrl they look invalid to the compiler.
Until something is done to bridge that slight mismatch (i.e. make the compiler super-clever so it can look for require.config sections and use them to check paths) it is easier not to set a baseUrl and use the full path in your import statements:
import vm = require('./scripts/bin/models/ViewModel');
Are you sure that the require call is done with [] and not just
require('models/ViewModel', function (viewModel) { // this is an error
console.log("test");
});
See : http://requirejs.org/docs/errors.html#requireargs
I am building a little node app and using Express with Jade and Stylus to render some basic HTMl pages.
I was curious if there is a way for me to pass some variables INTO the .styl file that are generated from Node? I am well aware that i can define variables inside of the .styl file but I have a need to be more dynamic. Specifically i was looking for an easy way to store some colors in the db, have node grab those values, then insert those into the .styl file so that when the page is rendered these variables are passed in. It seems like this should be do-able but i am lacking on the details. Any help is appreciated. Thanks!
Thanks to #ebohlman as his advice was close to what i ultimately implemented.
basically i was trying to figure out how to do this on top of the Connect Middleware and here is what i came up with:
when doing app.configure i used the custom compile compile function (key 'compile') like so:
app.use(require('stylus')
.middleware({
src: app.root + '/app/public',
compile: compile
})
);
then i created a couple of functions:
var stylus = require('stylus');
var mylib = function(style){
style.define('themeColor1', function(){
//call to the db that returns a color
color = 'blue';
color = color ? color : 'orange';
return new stylus.nodes.Literal(color);
});
};
var compile = function(str, path) {
return stylus(str)
.use(mylib);
};
then inside of the .styl file i do:
background-color themeColor1();
the ternary operator in the themeColor1 function allows for easy defaults and an override. It took me a bit to figure out the API based upon the examples but it seems like this COULD be a solution others would want to know how to do. If anyone has any downfalls of this approach please let me know.
You can use the Stylus API's define() function to set Stylus variables and make JS functions available to it.