Node.js/Express 不输出 console.log,highcharts.js 给出未定义的错误
Node.js/Express not outputting console.log, highcharts.js giving undefined error
我有 PHP 背景,但第一次尝试 Node/Express 网站。我已经成功地使用 express-generator 在本地主机上获取基本框架 运行。我还通过 npm 安装了 highcharts,并按照 instructions given by highcharts 通过 require() 将其添加到我的项目中。我现在在我的 index.js:
中有了这个
var express = require('express');
var Highcharts = require('highcharts');
var router = express.Router();
// Load module after Highcharts is loaded
require('highcharts/modules/exporting')(Highcharts);
console.log(Highcharts);
// Create the chart
Highcharts.chart('container', { /*Highcharts options*/ });
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
我有两个问题:
1) 当 运行 DEBUG=project:* npm start 时,console.log() 没有输出到终端或浏览器控制台。它是输出到我没有检查过的其他东西还是我需要做更多的事情才能看到这个?
2) 要求('highcharts/modules/exporting')(Highcharts);正在抛出类型错误:无法读取未定义的 属性 'document'
在 /Applications/MAMP/htdocs//node_modules/highcharts/modules/exporting.js:9:115
我哪里搞砸了?
看一下错误:
require('highcharts/modules/exporting')(Highcharts); is throwing TypeError: Cannot read property 'document' of undefined
它正在寻找 document
这是一个浏览器 API,HighCharts 只是客户端。
您链接到的 Highcharts 示例使用 require()
的原因是许多人 运行 他们的浏览器代码通过 Browserify, in order to keep using their favorite Node idioms and maintain some level of isomorphic design。
尽管如此,Highcharts 仍然适用于浏览器,不能在 Node.js 中使用。您的 Express 应用程序应该提供一个 HTML 页面,该页面本身通过某种方式加载 Highcharts。您不限于使用 Browserify,但它是此用例的流行选择。
此外,至于您不确定在哪里查找日志:始终是终端。您的 Node 程序将 永远不会 登录到浏览器控制台,除非您特意使用 Node Inspector.
等花哨的东西让它们这样做
我有 PHP 背景,但第一次尝试 Node/Express 网站。我已经成功地使用 express-generator 在本地主机上获取基本框架 运行。我还通过 npm 安装了 highcharts,并按照 instructions given by highcharts 通过 require() 将其添加到我的项目中。我现在在我的 index.js:
中有了这个var express = require('express');
var Highcharts = require('highcharts');
var router = express.Router();
// Load module after Highcharts is loaded
require('highcharts/modules/exporting')(Highcharts);
console.log(Highcharts);
// Create the chart
Highcharts.chart('container', { /*Highcharts options*/ });
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
我有两个问题:
1) 当 运行 DEBUG=project:* npm start 时,console.log() 没有输出到终端或浏览器控制台。它是输出到我没有检查过的其他东西还是我需要做更多的事情才能看到这个?
2) 要求('highcharts/modules/exporting')(Highcharts);正在抛出类型错误:无法读取未定义的 属性 'document'
在 /Applications/MAMP/htdocs//node_modules/highcharts/modules/exporting.js:9:115
我哪里搞砸了?
看一下错误:
require('highcharts/modules/exporting')(Highcharts); is throwing TypeError: Cannot read property 'document' of undefined
它正在寻找 document
这是一个浏览器 API,HighCharts 只是客户端。
您链接到的 Highcharts 示例使用 require()
的原因是许多人 运行 他们的浏览器代码通过 Browserify, in order to keep using their favorite Node idioms and maintain some level of isomorphic design。
尽管如此,Highcharts 仍然适用于浏览器,不能在 Node.js 中使用。您的 Express 应用程序应该提供一个 HTML 页面,该页面本身通过某种方式加载 Highcharts。您不限于使用 Browserify,但它是此用例的流行选择。
此外,至于您不确定在哪里查找日志:始终是终端。您的 Node 程序将 永远不会 登录到浏览器控制台,除非您特意使用 Node Inspector.
等花哨的东西让它们这样做