2016-11-28 377 views
0

我使用流星1.4.2.3,我想导入NodeJs网络模块?我试图在服务器端用import net from 'net';导入它,但那不起作用。 Net未定义。导入nodejs模块

import net from 'net'; 

export class Print { 
    private printAsync(callback){ 
    let client = new net.Socket(); 

    } 
} 

回答

0

从一个新鲜的meteor create项目,以下适用于我。

/server/main.js

import { Meteor } from 'meteor/meteor'; 

Meteor.startup(() => { 
    // code to run on server at startup 
}); 

import net from 'net'; 

export class Print { 
    printAsync(callback){ 
    let client = new net.Socket(); 
    console.log(client); 
    } 
} 

console.log(net); 
const print = new Print(); 
print.printAsync(); 

输出在控制台:

I20161129-07:59:18.007(-8)? { createServer: [Function], 
I20161129-07:59:18.008(-8)? createConnection: [Function], 
I20161129-07:59:18.008(-8)? connect: [Function], 
I20161129-07:59:18.008(-8)? _normalizeConnectArgs: [Function: normalizeConnectArgs], 
I20161129-07:59:18.008(-8)? Socket: { [Function: Socket] super_: { [Function: Duplex] super_: [Object] } }, 
I20161129-07:59:18.008(-8)? Stream: { [Function: Socket] super_: { [Function: Duplex] super_: [Object] } }, 
I20161129-07:59:18.009(-8)? Server: 
I20161129-07:59:18.009(-8)? { [Function: Server] 
[...] 
I20161129-07:59:18.012(-8)? Socket { 
I20161129-07:59:18.013(-8)? _connecting: false, 
I20161129-07:59:18.013(-8)? _hadError: false, 
I20161129-07:59:18.013(-8)? _handle: null, 
I20161129-07:59:18.013(-8)? _parent: null, 
I20161129-07:59:18.013(-8)? _host: null, 
I20161129-07:59:18.013(-8)? _readableState: 
I20161129-07:59:18.020(-8)? ReadableState { 
I20161129-07:59:18.020(-8)?  objectMode: false, 
I20161129-07:59:18.020(-8)?  highWaterMark: 16384, 
[...] 

我无法重现您不确定net

+0

我正在导入它在服务器端,但导入不起作用。 –

+0

呵呵,它适合我。你有样品回购还是你能提供更多的上下文? – kooc

+0

我更新了问题。变量网络是未定义的。你是如何导入网络模块的?你安装了npm包吗? –