小编Pat*_*ick的帖子

flatiron.js使用union,director和plate进行路由和模板化?

来自express.js,我想为一个小项目尝试熨斗.然而,有一些小问题使我无法真正到达某个地方.

var flatiron = require('flatiron')
,  session = require('connect').session
,  ecstatic = require('ecstatic')
,  path = require('path')
,  fs = require('fs')
,  plates = require('plates')
,  director = require('director')
,  winston = require('winston')
,  union = require('union');

var router = new director.http.Router();
var server = union.createServer({
  before: [
    ecstatic(__dirname + '/public')
  ]
});

router.get('/', function () {
  var self = this;
  fs.readFile('public/layout.html', 'utf-8', function(err, html) {
    [...]
  })
});

server.listen(3000, function () {
  console.log('Application is now started on …
Run Code Online (Sandbox Code Playgroud)

javascript url-routing node.js flatiron.js

7
推荐指数
1
解决办法
1860
查看次数

flatiron.js/plates - 如何使用模板和i18n?

我刚刚开始看盘子,因为很多人都在谈论它.

有一些带有少量html片段的板块的例子,但实际上并不是一个完整的模板文件.所以我想知道如何将布局分成layout.html文件并将内容分成几个content.html文件?

另外,我想知道flatiron.js/plate中是否有一些针对多语言网站的策略?

谢谢!

javascript templates node.js flatiron.js

7
推荐指数
1
解决办法
1032
查看次数

内联条件

  - if (typeof(person) == 'undefined')
    input(type="text", name="person[Name]")
  - else
    input(type="text", name="person[Name]", value="#{person.Name}")
Run Code Online (Sandbox Code Playgroud)

有没有办法写这个内联?我有一个选项选择,我不想为30+值做一个条件语句来选择正确的选项.

javascript node.js express pug

6
推荐指数
2
解决办法
6115
查看次数

使用node.js,nano和CouchDB进行身份验证

有没有办法在初始化后更改nano中的配置参数?我想初始纳米:

nano = require('nano')('http://127.0.0.1:5984')
Run Code Online (Sandbox Code Playgroud)

用户提交登录表单后,更改用户和密码.我总是收到一个错误:

nano.cfg.user = params.user.name
TypeError: Cannot set property 'user' of undefined
Run Code Online (Sandbox Code Playgroud)

或者我应该fork nano并编写auth函数来调整值?

authentication couchdb node.js couchdb-nano

5
推荐指数
2
解决办法
4805
查看次数

用身份验证中间件来表达资源?

Passport.js为node.js和Express提供了很好的身份验证,包括一个中间件解决方案:

ensureAuthenticated = function(req, res, next) {
  if (req.isAuthenticated()) {
    return next();
  }
  return res.redirect("/login");
};
Run Code Online (Sandbox Code Playgroud)

如何在快速资源模块中使用此中间件?不幸,

app.resource('users', ensureAuthenticated, require('./resources/users'));
Run Code Online (Sandbox Code Playgroud)

不起作用.

javascript middleware connect node.js express

5
推荐指数
2
解决办法
1万
查看次数

Browserify - 找不到变量:需要

我正在使用客户端上需要的几个 node.js 模块:

索引.js:

var sync = require('../lib/sync');
Run Code Online (Sandbox Code Playgroud)

所以我认为 browserify 可能是最好的选择。捆绑文件效果很好,但是 browserify 将所有内容都包装在(function(){my code here})();导致Can't find variable: require错误的 a 中。我怎样才能防止这种行为?

当我取下包装时,一切都按预期进行,没有错误。

javascript node.js browserify

5
推荐指数
1
解决办法
2082
查看次数

docker-compose .env与direnv .envrc

我们已经使用direnv已有一段时间了,以自动将环境变量加载到特定文件夹中。从版本3开始,docker -compose似乎支持.env文件

.envrcdirenv使用的文件使用export

export NODE_ENV=development

仅在没有export获取变量值的情况下,将同一文件与docker-compose一起使用似乎无效。

NODE_ENV=development

关于如何将其统一为单个.env.envrc文件或替代direnv的任何想法?

environment-variables docker docker-compose direnv

5
推荐指数
3
解决办法
1632
查看次数

在AngularJS和Testacular中测试广播

我正在使用angular-http-auth模块拦截401响应.event:auth-loginRequired如果有一个可以用$ on()接收的401响应,则该模块进行广播.但是我怎么测试呢?

beforeEach(inject(function($injector, $rootScope) {
  $httpBackend = $injector.get('$httpBackend');
  myApi = $injector.get('myApi');
  scope = $rootScope.$new();
  spyOn($scope, '$on').andCallThrough();
}));
describe('API Client Test', function() {
  it('should return 401', function() {
    $httpBackend.when('GET', myApi.config.apiRoot + '/user').respond(401, '');
    myApi.get(function(error, success) {
      // this never gets triggered as 401 are intercepted
    });
    scope.$on('event:auth-loginRequired', function() {
      // This works!
      console.log('fired');
    });

    // This doesn't work
    expect($scope.$on).toHaveBeenCalledWith('event:auth-loginRequired', jasmine.any(Function));

    $httpBackend.flush();
  });
});
Run Code Online (Sandbox Code Playgroud)

testing jasmine angularjs karma-runner

4
推荐指数
1
解决办法
2839
查看次数

'exit' - 在node.js集群中从master中杀死的工作进程中的事件

我想优雅地从主进程退出子进程,其中主代码和子代码在不同的.js文件中.

master.js

cluster.setupMaster({
  exec : 'child.js',
  args : [],
  silent : false
});
cluster.fork({'some': 'info'});

process.on('SIGINT', function() {
  console.log('\nshutting down services ...');
  function eachWorker(callback) {
    for (var id in cluster.workers) {
      callback(cluster.workers[id]);
    }
  }
  eachWorker(function(worker) {
    worker.kill();
  });
  process.exit(0);
})
Run Code Online (Sandbox Code Playgroud)

child.js

cluster.worker.on('disconnect', function() {
  console.log(cluster.worker.id + ' shutting down.');
})
Run Code Online (Sandbox Code Playgroud)

我尝试了各种信号,cluster.worker.on('exit'),process.on('exit'),process.on('disconnect')等,但似乎没有触发.有任何想法吗?

worker.send()在执行期间工作,但不在SIGINT函数内?

javascript node.js

4
推荐指数
1
解决办法
2825
查看次数

如何删除不需要的键和键值


这是我从CouchDB数据库中检索的原始JSON对象:

 {
  "total_rows": 3,
  "offset": 0,
  "rows": [
    {
      "id": "1e8fe199d4e761b71bb8920eb3003db2",
      "key": null,
      "value": {
        "_id": "1e8fe199d4e761b71bb8920eb3003db2",
        "_rev": "3-6979e0cd646f2bc0ddf84c0664d25808",
        "Room_number": 99,
        "Location": "City campus",
        "Timetable": {
          "Monday": [
            "08:30",
            "10:00",
            "11:30",
            "13:30",
            "14:14"
          ],
          "Tuesday": [
            "08:30",
            "10:00",
            "11:30",
            "13:30",
            "14:14"
          ],
          "Sunday": [
           "08:30",
            "10:00",
            "11:30",
            "13:30",
            "14:14"
          ]
        }
      }
    },
    {
      "id": "1e8fe199d4e761b71bb8920eb3004cc6",
      "key": null,
      "value": {
        "_id": "1e8fe199d4e761b71bb8920eb3004cc6",
        "_rev": "2-f7250cca62a804137174a20f48312c40",
        "Room_number": 12,
        "Location": "City hall",
        "Timetable": {
          "Monday": [
            "08:30",
            "10:00",
            "11:30", …
Run Code Online (Sandbox Code Playgroud)

javascript node.js

3
推荐指数
1
解决办法
1332
查看次数