导出默认路由;NodeJS 中的语法错误 [英] export default routes; SyntaxError in NodeJS

查看:54
本文介绍了导出默认路由;NodeJS 中的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

const routes = (app) => {
  app.route('/contact')
  .get((req, res, next) => {
      // middleware
      console.log(`Request from: ${req.originalUrl}`)
      console.log(`Request type: ${req.method}`)
      next();
    }, (req, res, next) => {
      res.send('GET request successful!!!!');
  })

  .post((req, res) =>
    res.send('POST request successful!!!!'));

  app.route('/contact/:contactId')
  .put((req, res) =>
    res.send('PUT request successful!!!!'))

  .delete((req, res) =>
    res.send('DELETE request successful!!!!'));
}

export default routes;

执行时产生这个错误:

export default routes;
^^^^^^

SyntaxError: Unexpected token export

我实际上是在尝试跟进培训视频,所以我对此有点陌生.根据我的理解,他正在尝试使用 ES6,我知道某些命令(例如 import)在 node ver 9 中本身不可用.这可能是其中之一吗?有其他选择吗?

I'm actually trying to follow along in a training video so I'm a bit new to this. From my understanding he's trying to use ES6 and I know some commands, like import, aren't available in node ver 9 natively. Could this be one of them? Any alternatives?

推荐答案

很可能您的 Node 项目没有设置为使用 ES6 模块加载.

Most likely your Node project is not setup to use ES6 module loading.

Node 使用旧的模块加载标准,称为 CommonJS 标准.为了让您的项目以您拥有的方式使用 ES6 模块加载,您需要使用 babel 和 webpack 之类的工具.

Node uses an older standard of module loading called the CommonJS standard. In order for your project to use the ES6 module loading the way you have it you need to use babel and a tool like webpack.

如果你搜索我的名字和教程,我会在不到 3 分钟的时间内展示如何进行设置.在这个例子中,它还设置了一个 React 项目,除此之外你只会对其他所有内容感兴趣.

If you search my name and tutorial I show how to set this up in less than 3 minutes. In the example, it's also setting up a react project, you would just be interested in everything else besides that.

这篇关于导出默认路由;NodeJS 中的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆