简体   繁体   English

如何在我们的节点项目中使用JavaScript项目?

[英]How to use a javascript project in our node project?

As I'm beginning to grasp the world of javascript as a client/server language (up to now it's all been C#), I would like to know how could I have an existing project as part of my own project so I can call it internally and not after it's loaded in the client. 当我开始掌握作为客户端/服务器语言的javascript世界(到现在为止都是C#)时,我想知道如何将现有项目作为自己项目的一部分,因此我可以称之为内部,而不是在客户端中加载之后。

using js-cookie project , and taken that I have: 使用js-cookie project ,并认为我有:

/libs
  app.ts
/dependencies
  js-cookie.js
/dist
 app.js
gruntfile.js
server.js
package.json

I would love to do: 我想做:

import * as JsCookies from './../dependencies/js-cookie.js'

export default class MyProject {

    get_cookie(name: string):string {
        return JsCookie.get(name);
    }

    set_cookie(name: string, value: string, expires?: number): void {
        if (typeof expires === "number")
            JsCookie.set(name, value, { expires: expires });
        else
            JsCookie.set(name, value);
    }

    // ...
}

in short, abstract my own project of handling cookies and use the js-cookie project for that, and let webpack minimize it and obfuscating it. 简而言之,请抽象我自己的处理cookie的项目 ,并为此使用js-cookie项目 ,并让webpack将其最小化并对其进行混淆。

How could I achieve such thing? 我怎样才能做到这一点?

Update: 更新:

in a terminal: 在终端中:

$ npm install js-cookie

Then in your code: 然后在您的代码中:

var Cookies = require('js-cookie');
Cookies.set(...);

Old irreleveant stuff: 旧的无用的东西:

As far as I can understand what you want to do, you'll have to export the class. 据我了解您想做什么,您必须导出该类。

// your class definition
var foo = function() {
};

module.exports = foo;

Then, in your other file, just do: 然后,在另一个文件中,只需执行以下操作:

var Foo = require('./../dependencies/js-cookie.js');
var bar = new Foo();
bar.get(...);
bar.set(...);

Update: 更新:

In the end, module.exports is the key to export data, functions, classes through node. 最后, module.exports是通过节点导出数据,函数,类的关键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM