そのうち完全に理解したら書き足します。たぶん。
やりたいこと
- Node.js v8 ~
- Express
- POST でリクエストボディに Shift_JIS な文字列が詰まってる
- 文字列を JavaScript で処理したいので UNICODE にしたい
やりかた
app.js
const express = require('express')
const bodyParser = require('body-parser')
const encoding = require('encoding-japanese')
const PORT = process.env.PORT || 3000
const app = express()
app.use(bodyParser.urlencoded({ extended: true }))
app.post('/', (req, res) => {
const decoded = encoding.urlDecode(req.body.message)
const buffer = Buffer.from(decoded)
const str = encoding.convert(buffer, {
to: 'UNICODE',
from: 'SJIS',
type: 'string'
})
})
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`)
})
です。