1
2

More than 3 years have passed since last update.

javascriptでcontent-typeを自動で取得する方法

Last updated at Posted at 2020-02-25

content-typeを自動で取得する方法を探した結果、mime-typesを使うことにしました。

前提条件

  • npmがインストールされていること

インストール

$ npm i --save mime-types

使い方

設定

touchコマンドでファイルを作成します。

$ touch test.js

mime-typesモジュールの読み込みと、引数にパスを入れてメソッドを使用します。

test.js
const mime = require('mime-types')

console.log(mime.lookup('test.json'))
console.log(mime.contentType('test.json'))

実行

nodeコマンドでtest.jsを実行します。

$ node test

出力結果

application/json
application/json; charset=utf-8

参考文献

この記事は以下の情報を参考にして執筆しました。

1
2
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
2