nodejs node HTTP通信

本稿について

本稿はサイト運営者が学んだことなどを記したメモの内容です。
全てが正確な情報とは限りません。ご注意ください。また修正するべき点は適時修正していきます
Promiseを使った通信をしたい場合
$ npm install request --save
$ npm install request-promise —-save

const request = require('request-promise');
const requestOptions = {
      url: `http://127.0.0.1:8000/hoge`,
      method: 'POST',
      headers: { "Content-Type": "application/json" },
      json: sendParams
}
let response = await request(requestOptions)

expressなどのrouting上でasync/awaitをする場合にはwrapperを使った方が良い。
// async wrapper
const asyncWrapper = fn => {
  return (req, res, next) => {
    return fn(req, res, next).catch(next);
  }
};

router.get('/auth', asyncWrapper(async(req, res, next) => {
}));

GETかそれ以外かでパラメータの取得が違う
req.query // GET
req.params // GET以外


[参考]



Nuxtで利用しようとした時に fs, net, tlsもインストールが必要だった。


$ npm run dev

Module Warning (from ./node_modules/eslint-loader/dist/cjs.js):                                                      errors 20:21:35

/Users/koibuchitetsuya/work/focusup-web/pages/index.vue
  10:9  warning  Require self-closing on Vue.js custom components (<work>)  vue/html-self-closing

✖ 1 problem (0 errors, 1 warning)
  0 errors and 1 warning potentially fixable with the `--fix` option.

                                                                                                                    errors 20:21:35
You may use special comments to disable some warnings.                                                              errors 20:21:35
Use // eslint-disable-next-line to ignore the next line.                                                            errors 20:21:35
Use /* eslint-disable */ to ignore all warnings in a file.                                                          errors 20:21:35

ERROR  Failed to compile with 7 errors                                                                              errors 20:21:35

These dependencies were not found:                                                                                  errors 20:21:35
                                                                                                                    errors 20:21:35
* fs in ./node_modules/request/lib/har.js                                                                            errors 20:21:35
* net in ./node_modules/forever-agent/index.js, ./node_modules/request-promise/node_modules/tough-cookie/lib/cookie.js and 2 others
* tls in ./node_modules/forever-agent/index.js, ./node_modules/tunnel-agent/index.js                                errors 20:21:35
                                                                                                                    errors 20:21:35
To install them, you can run: npm install --save fs net tls

インストール
$ npm install --save fs net tls

fsに関してはこれをnuxt.config.jsに入れないとダメだった

nuxt.config.js
// Build Configuration (https://go.nuxtjs.dev/config-build)
  build: {
    extend: function (config, {isDev, isClient}) {
      config.node = {
        fs: "empty"
      };
    }
  },

[参考]

関連ページ

nvm

#nodejs
Back