我正在尝试按照这些说明进行操作,在执行命令提示符步骤后,它会告诉我导入并创建 SparkPost 类的实例:
const SparkPost = require(‘sparkpost’)
const client = new SparkPost('YOUR API KEY')
但我不知道他们要我在哪里做。当然不是命令提示符。他们让我制作的模块中有几十个文件,所以我迷路了。请帮忙。
我也看过这里,但这让我很困惑。好像他们认为我应该已经知道了。
更新:我将此代码保存为 index.js。这是我的文件结构的屏幕截图
/*this is my code from the sparkpost tutorial*/
const SparkPost = require(‘sparkpost’);
const client = new SparkPost('XXX-my-api-XXX');
/*this is my code from the node.js tutorial mentioned in the comments*/
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
这是我尝试启动时的样子:
C:\Users\jacob\sparkpost-test>node index.js
C:\Users\jacob\sparkpost-test\index.js:1
(function (exports, require, module, __filename, __dirname) { const SparkPost = require(‘sparkpost’);
SyntaxError: Invalid or unexpected token
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:607:28)
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)
at bootstrap_node.js:608:3