我正在尝试使用Node版本6.2.1和我的一些代码.计划将大多数面向超回调的代码迁移到看起来更干净且可能表现更好的代码.
我不知道为什么,当我尝试执行节点代码时终端抛出错误.
helloz.js
(async function testingAsyncAwait() {
await console.log("Print me!");
})();
Run Code Online (Sandbox Code Playgroud)
日志 -
BOZZMOB-M-T0HZ:rest bozzmob$ node helloz.js
/Users/bozzmob/Documents/work/nextgennms/rest/helloz.js:1
(function (exports, require, module, __filename, __dirname) { (async function testingAsyncAwait() {
^^^^^^^^
SyntaxError: Unexpected token function
at Object.exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:513:28)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Function.Module.runMain (module.js:575:10)
at startup (node.js:160:18)
at node.js:456:3
BOZZMOB-M-T0HZ:rest bozzmob$ node -v
v6.2.1
Run Code Online (Sandbox Code Playgroud)
我错过了什么?请给我一些启示.
更新1:
我尝试使用Babel作为Quentin建议,但是,我仍然收到以下错误.
更新的代码 -
require("babel-core/register");
require("babel-polyfill");
(async function testingAsyncAwait() {
await console.log("Print …Run Code Online (Sandbox Code Playgroud) 我安装了nodejs 7.3.0,我有这个代码:
let getContent = function (url) {
// return new pending promise
return new Promise((resolve, reject) => {
// select http or https module, depending on reqested url
const lib = url.startsWith('https') ? require('https') : require('http');
const request = lib.get(url, (response) => {
// handle http errors
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error('Failed to load page, status code: ' + response.statusCode));
}
// temporary data holder
const body = [];
// on every content …Run Code Online (Sandbox Code Playgroud) 我一直在关注使用GraphQL 这个教程,它告诉我在我的src/index.js文件中写下这段代码:
const express = require('express');
const bodyParser = require('body-parser');
const {graphqlExpress, graphiqlExpress} = require('apollo-server-express');
const schema = require('./schema');
// 1
const connectMongo = require('./mongo-connector');
// 2
const start = async () => {
// 3
const mongo = await connectMongo();
var app = express();
app.use('/graphql', bodyParser.json(), graphqlExpress({
context: {mongo}, // 4
schema
}));
app.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Hackernews GraphQL server running on port ${PORT}.`)
});
};
// …Run Code Online (Sandbox Code Playgroud)