Skip to content
This repository was archived by the owner on Jul 10, 2019. It is now read-only.

Commit b83c16c

Browse files
committed
feat: get custom http headers
1 parent 8dd8fde commit b83c16c

File tree

5 files changed

+125
-2
lines changed

5 files changed

+125
-2
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
"@nuxtjs/pwa": "^3.0.0-beta.14",
5757
"@nuxtjs/sentry": "^2.3.1",
5858
"@types/webpack": "^4.4.26",
59+
"@ungap/url-search-params": "^0.1.2",
60+
"body-parser": "^1.18.3",
5961
"cross-env": "^5.2.0",
6062
"express": "^4.16.4",
6163
"js-cookie": "^2.2.0",
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<template lang="pug">
2+
div
3+
h1.title
4+
| custom-http-headers
5+
hr
6+
h2 data
7+
p {{ data }}
8+
hr
9+
h2 headers
10+
p {{ headers['from-server'] }}
11+
</template>
12+
13+
<script lang="ts">
14+
import * as querystring from 'querystring'
15+
import { Component, Vue } from 'nuxt-property-decorator'
16+
import axios from 'axios'
17+
import URLSearchParams from '@ungap/url-search-params'
18+
19+
@Component
20+
export default class UserAgent extends Vue {
21+
data: string = ''
22+
23+
async asyncData({ $axios }) {
24+
$axios.defaults.headers.post['post-header'] = 'post-header1' // for POST requests
25+
$axios.defaults.headers.common['common-header'] = 'common-header1' // for all requests
26+
27+
const params = new URLSearchParams()
28+
params.append('param1', 'value1')
29+
params.append('param2', 'value2')
30+
31+
const { data, headers, status, statusText, config } = await $axios.post(
32+
`http://localhost:5000/custom-headers`,
33+
{ hoge: 'hoge' },
34+
// x-www-form-urlencoded で渡したい場合は Browser と node.js でデータの作り方が変わってきます
35+
// https://github.com/axios/axios#using-applicationx-www-form-urlencoded-format
36+
// https://developer.mozilla.org/ja/docs/Web/HTTP/Methods/POST
37+
// Browser では URLSearchParams を使い、 node.js では querystring を使います
38+
// また、axiosでPOSTでRequestしているつもりがOPTIONSで送信している場合は、
39+
// https://qiita.com/hiryu/items/cdb606542d960402e592
40+
// このあたりを検討する必要があるかもしれません
41+
// process.server ? querystring.stringify({ foo: 'bar' }) : params,
42+
{
43+
headers: {
44+
header1: 'custom1'
45+
}
46+
}
47+
)
48+
49+
console.log(
50+
'headers:',
51+
headers,
52+
'status:',
53+
status,
54+
'statusText:',
55+
statusText,
56+
'config:',
57+
config
58+
)
59+
60+
return {
61+
data,
62+
headers
63+
}
64+
}
65+
}
66+
</script>
67+
68+
<style lang="scss" scoped>
69+
h2 {
70+
color: crimson;
71+
}
72+
</style>

src/pages/example/index.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
p
1212
nuxt-link(to='/example/ajax/schema')
1313
| schema
14+
p
15+
nuxt-link(to='/example/ajax/custom-http-headers')
16+
| custom-http-headers
1417
p
1518
nuxt-link(to='/example/c-01/e-02/', no-prefetch)
1619
| custom-path /c-01/e-02/
@@ -53,6 +56,9 @@
5356
p
5457
nuxt-link(to='/example/dynamic-import')
5558
| dynamic-import
59+
p
60+
nuxt-link(to='/example/user-agent')
61+
| user-agent
5662
</template>
5763

5864
<script lang="ts">

tools/server.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
const express = require('express')
22
const app = express()
33

4+
// https://blog.ryo4004.net/web/306/
5+
// method: post のために必須
6+
const bodyParser = require('body-parser')
7+
app.use(bodyParser.urlencoded({ extended: true }))
8+
app.use(bodyParser.json())
9+
410
// Example directories as static files
511
app.use(express.static('src/static'))
612

713
app.use((req, res, next) => {
814
res.header('Access-Control-Allow-Origin', '*')
915
res.header(
1016
'Access-Control-Allow-Headers',
11-
'Origin, X-Requested-With, Content-Type, Accept'
17+
// 'origin, x-requested-with, content-type, accept'
18+
'*'
1219
)
20+
// https://stackoverflow.com/questions/37897523/axios-get-access-to-response-header-fields
21+
// https://github.com/axios/axios/issues/606
22+
// Access-Control-Expose-Headers を追加しないとカスタムレスポンスヘッダーをブラウザに返すことはできない
23+
res.header('Access-Control-Expose-Headers', 'from-server')
1324
next()
1425
})
1526

27+
/**
28+
* get '/'
29+
*/
1630
app.get('/', function(req, res) {
1731
res.send('Hello World')
1832
})
1933

34+
/**
35+
* get '/api-waiting-for-5-seconds'
36+
*/
2037
app.get('/api-waiting-for-5-seconds', function(req, res) {
2138
setTimeout(() => {
2239
res.send(
@@ -30,4 +47,25 @@ app.get('/api-waiting-for-5-seconds', function(req, res) {
3047
}, 5000)
3148
})
3249

50+
/**
51+
* get '/headers'
52+
*/
53+
app.get('/headers', (req, res) => {
54+
res.send(JSON.stringify(req.headers))
55+
})
56+
57+
/**
58+
* post '/custom-headers'
59+
*/
60+
app.post('/custom-headers', (req, res) => {
61+
console.log(req.body)
62+
63+
// カスタムレスポンスヘッダーをセットします
64+
res.set({
65+
'from-server': 'server1'
66+
})
67+
68+
res.send(JSON.stringify(req.headers))
69+
})
70+
3371
app.listen(5000)

yarn.lock

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2489,6 +2489,11 @@
24892489
lodash.unescape "4.0.1"
24902490
semver "5.5.0"
24912491

2492+
"@ungap/url-search-params@^0.1.2":
2493+
version "0.1.2"
2494+
resolved "https://registry.yarnpkg.com/@ungap/url-search-params/-/url-search-params-0.1.2.tgz#8ba8c0527543fe675d1c29ae0a2daca842e8ee4f"
2495+
integrity sha512-WVk5+lJ+AoNLh2sIDMhnEAgLsVQuI067hWLJCzirErH1GYiy1gs09q4+XZxYWSvdAsslKsaO4q1iXXMx2c72dA==
2496+
24922497
"@vue/babel-helper-vue-jsx-merge-props@^1.0.0-beta.2":
24932498
version "1.0.0-beta.2"
24942499
resolved "https://registry.yarnpkg.com/@vue/babel-helper-vue-jsx-merge-props/-/babel-helper-vue-jsx-merge-props-1.0.0-beta.2.tgz#f3e20d77b89ddb7a4b9b7a75372f05cd3ac22d92"
@@ -3528,7 +3533,7 @@ bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0:
35283533
version "4.11.8"
35293534
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f"
35303535

3531-
body-parser@1.18.3:
3536+
body-parser@1.18.3, body-parser@^1.18.3:
35323537
version "1.18.3"
35333538
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.18.3.tgz#5b292198ffdd553b3a0f20ded0592b956955c8b4"
35343539
dependencies:

0 commit comments

Comments
 (0)