我正在学习Nodejs作为一个完整的初学者,并从重建Conway的生命游戏的任务开始。目前我只是希望能够在浏览器/控制台中绘制网格以便开始,但我遇到了一些困难。
我已经学习了一些教程,这是我到目前为止所提出的: main.js
var http = require(http);
var InitGrid = require('./InitGrid.js');
http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(console.log(InitGrid.InitGrid()));
res.end();
}).listen(8080);
InitGrid.js
function make2DArray(GridWidth, GridHeight){
let arr = new Array(GridHeight);
for (let i=0; i < arr.length; i++){
arr[i] = new Array(GridWidth);
}
return arr;
}
let grid;
let GridWidth = 50;
let GridHeight = 50;
function InitGrid() {
grid = make2DArray(GridWidth, GridHeight);
for (let i=0; i < GridWidth; i++) {
for (let j=0; j < GridHeight; j++) {
grid[i][j] = floor(random(2));
}
}
}
当我使用'node main.js'运行上面的代码时,我收到以下错误:
assert.js:42
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: missing path
at Module.require (module.js:585:3)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\Users\Sara\Downloads\gameoflife\javascript-node\GameOfLife\script\main.js:1:74)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)