Skip to content

Commit bbf62a5

Browse files
authored
docs: simplify code examples with header comments (#386)
1 parent f442e77 commit bbf62a5

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

README.md

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ var express = require('express')
3838
var cors = require('cors')
3939
var app = express()
4040

41+
// Adds headers: Access-Control-Allow-Origin: *
4142
app.use(cors())
4243

4344
app.get('/products/:id', function (req, res, next) {
44-
res.json({msg: 'This is CORS-enabled for all origins!'})
45+
res.json({msg: 'Hello'})
4546
})
4647

4748
app.listen(80, function () {
48-
console.log('CORS-enabled web server listening on port 80')
49+
console.log('web server listening on port 80')
4950
})
5051
```
5152

@@ -56,12 +57,13 @@ var express = require('express')
5657
var cors = require('cors')
5758
var app = express()
5859

60+
// Adds headers: Access-Control-Allow-Origin: *
5961
app.get('/products/:id', cors(), function (req, res, next) {
60-
res.json({msg: 'This is CORS-enabled for a Single Route'})
62+
res.json({msg: 'Hello'})
6163
})
6264

6365
app.listen(80, function () {
64-
console.log('CORS-enabled web server listening on port 80')
66+
console.log('web server listening on port 80')
6567
})
6668
```
6769

@@ -79,12 +81,13 @@ var corsOptions = {
7981
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
8082
}
8183

84+
// Adds headers: Access-Control-Allow-Origin: http://example.com, Vary: Origin
8285
app.get('/products/:id', cors(corsOptions), function (req, res, next) {
83-
res.json({msg: 'This is CORS-enabled for only example.com.'})
86+
res.json({msg: 'Hello'})
8487
})
8588

8689
app.listen(80, function () {
87-
console.log('CORS-enabled web server listening on port 80')
90+
console.log('web server listening on port 80')
8891
})
8992
```
9093

@@ -118,12 +121,13 @@ var corsOptions = {
118121
}
119122
}
120123

124+
// Adds headers: Access-Control-Allow-Origin: <matched origin>, Vary: Origin
121125
app.get('/products/:id', cors(corsOptions), function (req, res, next) {
122-
res.json({msg: 'This is CORS-enabled for an allowed domain.'})
126+
res.json({msg: 'Hello'})
123127
})
124128

125129
app.listen(80, function () {
126-
console.log('CORS-enabled web server listening on port 80')
130+
console.log('web server listening on port 80')
127131
})
128132
```
129133

@@ -141,13 +145,13 @@ var express = require('express')
141145
var cors = require('cors')
142146
var app = express()
143147

144-
app.options('/products/:id', cors()) // enable pre-flight request for DELETE request
148+
app.options('/products/:id', cors()) // preflight for DELETE
145149
app.del('/products/:id', cors(), function (req, res, next) {
146-
res.json({msg: 'This is CORS-enabled for all origins!'})
150+
res.json({msg: 'Hello'})
147151
})
148152

149153
app.listen(80, function () {
150-
console.log('CORS-enabled web server listening on port 80')
154+
console.log('web server listening on port 80')
151155
})
152156
```
153157

@@ -181,28 +185,30 @@ Here’s an example that handles both public routes and restricted, credential-s
181185
var dynamicCorsOptions = function(req, callback) {
182186
var corsOptions;
183187
if (req.path.startsWith('/auth/connect/')) {
188+
// Access-Control-Allow-Origin: http://mydomain.com, Access-Control-Allow-Credentials: true, Vary: Origin
184189
corsOptions = {
185-
origin: 'http://mydomain.com', // Allow only a specific origin
186-
credentials: true, // Enable cookies and credentials
190+
origin: 'http://mydomain.com',
191+
credentials: true
187192
};
188193
} else {
189-
corsOptions = { origin: '*' }; // Allow all origins for other routes
194+
// Access-Control-Allow-Origin: *
195+
corsOptions = { origin: '*' };
190196
}
191197
callback(null, corsOptions);
192198
};
193199

194200
app.use(cors(dynamicCorsOptions));
195201

196202
app.get('/auth/connect/twitter', function (req, res) {
197-
res.send('CORS dynamically applied for Twitter authentication.');
203+
res.send('Hello');
198204
});
199205

200206
app.get('/public', function (req, res) {
201-
res.send('Public data with open CORS.');
207+
res.send('Hello');
202208
});
203209

204210
app.listen(80, function () {
205-
console.log('CORS-enabled web server listening on port 80')
211+
console.log('web server listening on port 80')
206212
})
207213
```
208214

0 commit comments

Comments
 (0)