1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Express】ESM方式でimportするときにルーターを使う方法

Last updated at Posted at 2022-06-19

なぜESM方式を使ってるのか

ESM Moduleを使おうとしてpackage.jsonに"type": "module"とやっているとrequireが使えないので

やり方

まずは普通にimportでやる

index.js
import hoge from "./hoge.js";

app.use("/hoge", hoge);

お次にhoge.jsを書いていく

hoge.js
import express from "express";
var router = express.Router(); //ルーターを生成

router.get("/puyo", function(req, res){
//なんかの処理
})

export default router;

完成

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?