简体   繁体   English

导出默认路由; NodeJS 中的语法错误

[英]export default routes; SyntaxError in 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;

Produces this error when executing:执行时产生此错误:

export default routes;
^^^^^^

SyntaxError: Unexpected token export

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.据我了解,他正在尝试使用 ES6,而且我知道一些命令,例如 import,在 node ver 9 中本身是不可用的。 Could this be one of them?这可能是其中之一吗? Any alternatives?有什么选择吗?

Most likely your Node project is not setup to use ES6 module loading.很可能您的 Node 项目没有设置为使用 ES6 模块加载。

Node uses an older standard of module loading called the CommonJS standard. Node 使用一种较旧的模块加载标准,称为 CommonJS 标准。 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.为了让您的项目以您拥有的方式使用 ES6 模块加载,您需要使用 babel 和类似 webpack 的工具。

If you search my name and tutorial I show how to set this up in less than 3 minutes.如果你搜索我的名字和教程,我会在不到 3 分钟的时间内展示如何设置它。 In the example, it's also setting up a react project, you would just be interested in everything else besides that.在这个例子中,它还建立了一个反应项目,除此之外你只会对其他一切感兴趣。

Try to use module.exports.routes;尝试使用module.exports.routes;

You want to use ES6 Module syntax.你想使用 ES6 模块语法。 This means that there should be support for ES6 Module syntax.这意味着应该支持 ES6 模块语法。

You can proceed with at least two way:您至少可以通过两种方式继续:

  • Use babel npm package to transpile ES6 style使用 babel npm 包转译 ES6 风格
  • Refactor to use syntax above.重构以使用上述语法。

You need to create a .babelrc file in your root folder as same as server.js or index.js and add and object like this:您需要在根文件夹中创建一个与 server.js 或 index.js 相同的 .babelrc 文件,并像这样添加和对象:

{
    "presets": [
        "env",
        "stage-0"
    ]
}

this will tell node to use the preset env and stage-o.这将告诉节点使用预设的 env 和 stage-o。 I assume that you download the babel plugin.我假设您下载了 babel 插件。

Just in case remember config the server.js (in my case) with this:以防万一记住用这个配置 server.js(在我的例子中):

"scripts": {
    "start": "nodemon ./server.js --exec babel-node -e js"
  },

to use ES6 Module syntax.使用 ES6 模块语法。

Node version 8.xx does not support import.节点版本 8.xx 不支持导入。 Use the latest version of node 9.xx and the error will go away.使用最新版本的node 9.xx,错误就会消失。 Also, you can us babel to transpile your code.此外,您可以使用 babel 来转译您的代码。

I encountered the same problem laterally i realized i have used the require in place of import ES6 syntax for importing the dependencies. 我从侧面遇到了相同的问题,我意识到我已经使用require代替了import ES6语法来导入依赖项。 I just solved it by using module.exports=router 我刚刚使用module.exports=router解决了

NOTE: Incase you use ES6 Importing, then the code you wrote in the question will work perfect. 注意:如果您使用ES6导入,那么您在问题中编写的代码将可以正常使用。 Here you can find an example of my Code. 在这里您可以找到我的代码示例。

const express = require('express');
let router = express.Router();

router.post('/', (req, res) => {
 res.status(201).json({
     success: true
  });
});
// make the export
module.exports = router;

You need to create in your file as same as index.js and add您需要在文件中创建与 index.js 相同的文件并添加

module.exports = router;

Add This in index.js在 index.js 中添加这个

const postRoutes=require('./routes/posts')
app.use('/post',postRoutes);

you have to add "type": "module" just before "scripts" in your package.json and change every // const express=require('express');您必须在 package.json 中的“脚本”之前添加“类型”:“模块”并更改每个 // const express=require('express'); to //import express from 'express' to //从'express'导入快递

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM