我有一个由NodeJS服务的Ang api到AngularJS前端.
我合作users:
GET /api/users #Returns all users
POST /api/users #Create new user
GET /api/users/:id #Return a user
PUT /api/users/:id #Edit a user
DELTE /api/users/:id #Delete a user
Run Code Online (Sandbox Code Playgroud)
这是一个用户:
{
login : "admin"
email : "admin@admin.com"
pass : "hashedpassword"
...
}
Run Code Online (Sandbox Code Playgroud)
我的用户可以属于 groups
GET /api/users/:id/groups #Return the groups of a user
Run Code Online (Sandbox Code Playgroud)
他们也可以拥有constraints或可以继承constraints他们的团体
GET /api/users/:id/constraints #Return the constraints of a user
GET /api/groups/:id/constraints #Return the constraints of a group
Run Code Online (Sandbox Code Playgroud)
问题 :
我正在创建一个管理页面,显示所有用户,他们的组,他们的约束.
我是不是该 :
groups.js
class groupsCtrl {
constructor() {
this.info = "test";
}
get(res, req) {
console.log("LOG ! ", JSON.stringify(this));
}
}
module.exports = new groupsCtrl(); //singleton
Run Code Online (Sandbox Code Playgroud)
routes.js
var express = require('express');
var router = express.Router();
var groupsCtrl = require('controllers/api_admin/groups.js');
router.get('/groups/', groupsCtrl.get);
Run Code Online (Sandbox Code Playgroud)
这个日志 LOG ! undefined
如何this在我的控制器类中访问?
我有一个像这样的功能(简化):
doSmthg (name, age, callback) {
callback(name, age);
}
Run Code Online (Sandbox Code Playgroud)
如果没有提供,我希望有一个年龄的默认值.
我知道我可以doSmthg(name, callback, age=42) {...}在ES6中做,但我被告知回调应该始终是最后一个参数,因为它使对函数的调用更具可读性.
目前我找到的解决方案是执行以下操作:
doSmthg (name, age, callback) {
if (arguments.length === 2) {
age = 42;
callback = age;
}
}
Run Code Online (Sandbox Code Playgroud)
但我觉得这个解决方案难以阅读.
这是好的解决方案吗?还有更好的吗?
在学校练习
我有这个功能
bar :: Float -> Float -> Float
bar x 0 = 0
bar 0 y = 0
bar x y = x * y
Run Code Online (Sandbox Code Playgroud)
我在GHC中键入它
let bar x 0 = 0; bar 0 y = 0; bar x y = x * y
Run Code Online (Sandbox Code Playgroud)
并评估
bar foo 0
bar 0 foo
Run Code Online (Sandbox Code Playgroud)
我被要求修改吧使用'|' 所以我想做一些像:
let bar x y = | x 0 = 0 | 0 y = 0 | x y = x * y
Run Code Online (Sandbox Code Playgroud)
但在ghci,我得到了
parse error on …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 ESLint 在项目中强制执行一种编码风格,但我有很多Irregular whitespace not allowed由这样的代码引起的错误:
var a = b || 10; //note the 2 spaces between || and 10
if (a === 10) { // again, 2 spaces between ) and {
Run Code Online (Sandbox Code Playgroud)
我没有找到任何方法可以轻松地为我的所有文件解决这个问题。我认为基本的正则表达式不起作用,因为在某些情况下双空格可能有效(JSON 对象的列缩进、软制表符缩进、字符串)。
我尝试了诸如 js-beautify 之类的工具,但没有找到解决此问题的任何选项。有什么工具可以帮助我吗?
我正在使用supertest测试我的API
我想检查我的CSRF令牌保护是否有效,然后为其他测试禁用它.
为此我设置NODE_ENV为test或not_test
app.js
var csrf = require('csurf');
var app = express();
if (process.env.NODE_ENV !== 'test') {
app.use(csrf({ cookie: true }));
app.use(function(req, res, next) {
res.cookie('XSRF-TOKEN', req.csrfToken());
return next();
});
}
Run Code Online (Sandbox Code Playgroud)
测试CSRF
process.env.NODE_ENV = 'not_test';
var app = require("app.js");
var request = require('supertest')(app);
var testAccount = {
"login": "test",
"pass": "test"
};
describe('CSRF protection', function() {
it('On /login', function(done){
request
.post('/login')
.send(testAccount)
.expect(403, done);
});
});
Run Code Online (Sandbox Code Playgroud)
测试登录 NODE_ENV现在正在测试
process.env.NODE_ENV = 'test';
var …Run Code Online (Sandbox Code Playgroud) 我有以下快速路线:
app.get('/:id', function (req, res) {
var idNum: number = Number(req.params.id);
var idCast: number = +req.params.id;
var id: number = req.params.id;
console.log('typeof idNum ', typeof idNum , ' ', 'idNum== 0 : ', idNum== 0 , ' ', 'idNum=== 0 : ', idNum=== 0);
console.log('typeof idCast', typeof idCast, ' ', 'idCast == 0 :', idCast == 0, ' ', 'idCast === 0 :', idCast === 0);
console.log('typeof id ', typeof id , ' ', 'id == 0 : ', id …Run Code Online (Sandbox Code Playgroud) 使用GHCi我执行以下操作:
prelude> let lol [] = []
prelude> let lol (x:xs) = (lol xs) ++ [x]
Run Code Online (Sandbox Code Playgroud)
当我尝试评估时
prelude> lol [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
我明白了
Exception: <interactive>:3:5-32: Non-exhaustive patterns in function lol
Run Code Online (Sandbox Code Playgroud)
我想我理解这个问题(列表中有1个元素不匹配?)但是看不出为什么他不能将x:xs匹配为x:[]
我正在尝试制作一个脚本来自动安装程序并在我的Fedora 19 linux发行版上配置它们.为了创建它,我创建了一个VM,我在"终端"应用程序中手动输入所有命令.
我希望能够记录我输入的所有内容和所有输出(stdin&stderr&stdout,如果我理解的话),所以我可以使用这个日志来制作我的脚本.
有没有办法做到这一点 ?
我试图在linux fedora 20上转储正在运行的进程的堆栈(我想自己做这个没有gdb/...)
但看起来我的缓冲区(buf)的默认值永远不会被替换,即使/ proc/x/mem的read()返回正数.
我禁用了selinux
[me@localhost ~]$ cat /proc/2419/maps
00400000-00401000 r-xp 00000000 fd:02 164622 /home/me/memo
00600000-00601000 r--p 00000000 fd:02 164622 /home/me/memo
00601000-00602000 rw-p 00001000 fd:02 164622 /home/me/memo
7fd7cbea2000-7fd7cc056000 r-xp 00000000 fd:02 8636 /usr/lib64/libc-2.18.so
7fd7cc056000-7fd7cc256000 ---p 001b4000 fd:02 8636 /usr/lib64/libc-2.18.so
7fd7cc256000-7fd7cc25a000 r--p 001b4000 fd:02 8636 /usr/lib64/libc-2.18.so
7fd7cc25a000-7fd7cc25c000 rw-p 001b8000 fd:02 8636 /usr/lib64/libc-2.18.so
7fd7cc25c000-7fd7cc261000 rw-p 00000000 00:00 0
7fd7cc261000-7fd7cc281000 r-xp 00000000 fd:02 723 /usr/lib64/ld-2.18.so
7fd7cc469000-7fd7cc46c000 rw-p 00000000 00:00 0
7fd7cc47d000-7fd7cc480000 rw-p 00000000 00:00 0
7fd7cc480000-7fd7cc481000 r--p 0001f000 fd:02 723 /usr/lib64/ld-2.18.so
7fd7cc481000-7fd7cc482000 …Run Code Online (Sandbox Code Playgroud) node.js ×5
javascript ×4
express ×2
haskell ×2
linux ×2
angularjs ×1
api ×1
bash ×1
c ×1
ecmascript-6 ×1
eslint ×1
js-beautify ×1
mocha.js ×1
rest ×1
superagent ×1
supertest ×1
typescript ×1