对于 Express.js 4.x,我无法确定是否应该返回响应(或下一个函数),因此:
这个:
app.get('/url', (req, res) => {
res.send(200, { message: 'ok' });
});
Run Code Online (Sandbox Code Playgroud)
或这个:
app.get('/url', (req, res) => {
return res.send(200, { message: 'ok' });
});
Run Code Online (Sandbox Code Playgroud)
有什么区别?
我正在进入System.js和JSPM,我已经到了想要将我的TypeScript源代码捆绑到JavaScript包中的地步.
现在,我可以将生成的JavaScript代码捆绑为:
jspm bundle some/source/path someDestFile.js
但是我需要首先将我的所有TypeScript预先构建到JavaScript中,然后捆绑,找到自己留下的所有已编译(和分离)的JS文件.这远非理想!
我在这里浏览了jspm文档,但没有找到解决方案.
为了清楚起见,我不想在我的浏览器中编译TypeScript,而是预编译的实体JavaScript包.
我该怎么做呢?
PS我用的打字稿transpiler安装所看到这里
使用时的npm_package_config_<variable>
用法因操作系统而异。
package.json(Linux 和 Windows)
config: {
foo: "bar"
}
Run Code Online (Sandbox Code Playgroud)
然后用于使用:
Linux
node app.js --arg=$npm_package_config_foo
Run Code Online (Sandbox Code Playgroud)
视窗
node app.js --arg=%npm_package_config_foo%
Run Code Online (Sandbox Code Playgroud)
因此,我创建了 2 个单独的scripts
,package.json
但这感觉很麻烦。
有什么办法可以跨平台吗?
PS我知道cross-env插件,但这不适用于上述情况,仅适用于NODE_ENV。
更新
为了避免对上下文的误解,这是我正在寻找的一个真实案例场景,将其用于 Docker 命令以指定端口号:
包.json
"config": {
"port": "3000"
}
...
"scripts": {
"docker:build": "docker build --build-arg PORT=$npm_package_config_port -t my-app .",
"docker:build:win": "docker build --build-arg PORT=%npm_package_config_port% -t my-app .",
}
Run Code Online (Sandbox Code Playgroud) 我有一个Directive
可以放置元素来检查相关元素的当前滚动位置.
看起来像这样:
@Directive({
selector: '[my-scroll-animation]'
})
Run Code Online (Sandbox Code Playgroud)
每当位置满足某个阈值时,我希望元素使用动画显示在屏幕上.我知道,Component
我可以附加animations
属性以及属性中的一些设置host
来激活动画.
我想要这样的东西:
import { myScrollAnimation } from './animations';
@Directive({
selector: '[my-scroll-animation]'
animations: [myScrollAnimation] // <- not possible?
})
Run Code Online (Sandbox Code Playgroud)
如何在指令中实现这一目标?
使用:Angular 4.0.0-rc.4
I have a folder structure for a Node.js /w Angular.js project with some files like so (from project root):
frontend
frontend-file1.ts
frontend-file2.ts
backend
backend-file1.ts
backend-file2.ts
Run Code Online (Sandbox Code Playgroud)
I use a TypeScript compiler along with many other gulp
plugins to compile this into a build
folder like so (notice how frontend
files get placed into public
):
build
backend-file1.js
backend-file2.js
public
frontend-file1.js
frontend-file2.js
Run Code Online (Sandbox Code Playgroud)
In the source folders, I use ES6/TypeScript import
statements to import files.
Example: backend-file1.ts
import './backend-file2';
Run Code Online (Sandbox Code Playgroud)
Situation
I've written some …
所以我有一个指令,需要输入:
@Directive({
selector: '[my-directive]'
})
export class MyDirective {
@Input('some-input')
public someInput: string;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,输入应该是一个string
值.现在,我想为这个指令编写测试,我想测试输入是否满足string
类型:
describe('MyDirective', () => {
let fixture: ComponentFixture<DummyComponent>;
let dummy: DummyComponent;
let directive: DebugElement;
beforeEach(async(() => {
TestBed
.configureTestingModule({
imports: [
MyModule.forRoot()
],
declarations: [
DummyComponent
]
})
.compileComponents();
fixture = TestBed.createComponent(DummyComponent);
dummy = fixture.componentInstance;
directive = fixture.debugElement.query(By.directive(MyDirective));
fixture.detectChanges();
}));
it('should satisfy only a string input and error on other inputs', () => {
// How to test?
});
});
@Component({
selector: …
Run Code Online (Sandbox Code Playgroud) 我一直在尝试缓存node_modules
Docker构建.我尝试了几种方法,包括这里的方法,但没有成功.
我缓存的主要原因是因为构建我的图像需要30多分钟,这太过分了.
我的Dockerfile
:
# This image will be based on the oficial nodejs docker image
FROM node:4.2.1
RUN npm install -g jspm@0.17.0-beta.7 && \
npm install -g gulp && \
npm install -g tsd
# Use changes to package.json to force Docker not to use the cache
# when we change our application's nodejs dependencies:
ADD package.json /src/package.json
RUN cd /src && npm install
# Put all our code inside that directory …
Run Code Online (Sandbox Code Playgroud) 我使用Webpack和插件html-webpack-plugin
.基于环境变量,我想<meta></meta>
在最终index.html
文件中注入一个标签.
我该怎么做呢?
上下文
我有一个 GraphQL API 和一个 NodeJS & Angular 应用程序,其中包含一个包含用户的 MongoDB 数据库。对于每个用户,都有一个包含公共信息的公共页面,例如id
和username
。当用户登录时,会有一个包含扩展信息的私人资料页面,例如email
.
仅就上下文而言,我使用jsonwebtoken和accesscontrol来对用户进行身份验证和授权。该信息存储在每个 GraphQL 解析函数的上下文中,因此可以使用识别登录用户所需的任何信息。
我有一个 GraphQL 查询,它检索公共用户,如下所示:
query getUserById($id: ID!) {
getUserById(id: $id) {
id,
username
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试考虑检索公共用户或私人用户的正确实现。由于 GraphQL 是强类型的,我在想出合适的解决方案时遇到了一些麻烦。
问题
如何区分公共用户和私人用户?
注意事项
1. 单独查询
因此,选项之一是对公共和私有字段进行单独查询:
公开查询
query getUserById($id: ID!) {
getUserById(id: $id) {
id,
username
}
}
Run Code Online (Sandbox Code Playgroud)
私人查询
query getMe {
getMe {
id,
username,
email
}
}
Run Code Online (Sandbox Code Playgroud)
2. 使用 GraphQL 接口
我看到了这篇 Medium 文章,它解释了如何使用 …
access-control node.js role-based-access-control graphql graphql-js
我有一个Angular 4 Universal应用程序,我想开始使用JSON-LD格式的微数据.
它使用script
带有一些内容的标签,例如:
<script type="application/ld+json">
{
"@context": "http://schema.org",
...
}
</script>
Run Code Online (Sandbox Code Playgroud)
由于此数据应该按视图更改,我正在寻找一种方法在Angular 4中将路径更改注入此数据.目前,脚本标记从模板中删除.使用变通方法时docuemnt.createElement
,这不适用于服务器端的Angular Universal应用程序.
我该怎么做?
编辑
我使用Angular 4.xx,现在称为普通Angular.我document
是这样注射的:
import { DOCUMENT } from '@angular/platform-browser';
class Test {
constructor(@Inject(DOCUMENT) private _document) {
}
public createScriptTag() {
this._document.createElement('script'); // doesn't work server-side
}
}
Run Code Online (Sandbox Code Playgroud) javascript ×7
node.js ×6
typescript ×6
angular ×3
angular4 ×1
angularjs ×1
docker ×1
express ×1
graphql ×1
graphql-js ×1
gulp ×1
jasmine ×1
jspm ×1
npm ×1
systemjs ×1
testing ×1
tutum ×1
unit-testing ×1
webpack ×1