Mapping file suffixes to Content-Type
npm install is-assets
or
yarn add is-assets
HTTP传输文件时根据静态文件名后缀判定Content-Type,便于文件接收方或浏览器解析内容
直接使用npm包自带的类型map
js代码示例
const { assetsMapping, ASSETS_TYPE } = require('is-assets');
console.log(ASSETS_TYPE); // npm包自带的静态类型映射关系
const jsType = assetsMapping('js') // application/x-javascript
const jpgType = assetsMapping('jpg') // image/jpeg
const mp4Type = assetsMapping('mp4') // undefined
ts代码示例
import { assetsMapping, ASSETS_TYPE } from 'is-assets';
console.log(ASSETS_TYPE); // npm包自带的静态类型映射关系
const jsType = assetsMapping('js') // application/x-javascript
const jpgType = assetsMapping('jpg') // image/jpeg
const mp4Type = assetsMapping('mp4') // undefined
扩展npm包的类型map
js代码示例
const customMap = {
txt: 'text/plain',
mp4: 'video/mp4'
};
const { assetsMapping } = require('is-assets');
const MyAssetsMapping = assetsMapping(customMap);
const jsType = MyAssetsMapping('js') // application/x-javascript
const txtType = MyAssetsMapping('txt') // text/plain
const mp4Type = MyAssetsMapping('mp4') // video/mp4
ts代码示例
const customMap = {
txt: 'text/plain',
mp4: 'video/mp4'
};
import { assetsMapping } from 'is-assets';
const MyAssetsMapping = assetsMapping(customMap);
const jsType = MyAssetsMapping('js') // application/x-javascript
const txtType = MyAssetsMapping('txt') // text/plain
const mp4Type = MyAssetsMapping('mp4') // video/mp4