MCPcopy Index your code
hub / github.com/restify/node-restify / serve

Function serve

lib/plugins/static.js:167–219  ·  view source on GitHub ↗
(req, res, next)

Source from the content-addressed store, hash-verified

165 }
166
167 function serve(req, res, next) {
168 var file;
169
170 if (opts.file) {
171 //serves a direct file
172 file = path.join(opts.directory, decodeURIComponent(opts.file));
173 } else if (opts.appendRequestPath) {
174 file = path.join(opts.directory, decodeURIComponent(req.path()));
175 } else {
176 var dirBasename = path.basename(opts.directory);
177 var reqpathBasename = path.basename(req.path());
178
179 if (
180 path.extname(req.path()) === '' &&
181 dirBasename === reqpathBasename
182 ) {
183 file = opts.directory;
184 } else {
185 file = path.join(
186 opts.directory,
187 decodeURIComponent(path.basename(req.path()))
188 );
189 }
190 }
191
192 if (req.method !== 'GET' && req.method !== 'HEAD') {
193 next(new MethodNotAllowedError('%s', req.method));
194 return;
195 }
196
197 if (!re.test(file.replace(/\\/g, '/'))) {
198 next(new NotAuthorizedError('%s', req.path()));
199 return;
200 }
201
202 if (opts.match && !opts.match.test(file)) {
203 next(new NotAuthorizedError('%s', req.path()));
204 return;
205 }
206
207 if (opts.gzip && req.acceptsEncoding('gzip')) {
208 fs.stat(file + '.gz', function stat(err, stats) {
209 if (!err) {
210 res.setHeader('Content-Encoding', 'gzip');
211 serveFileFromStats(file, err, stats, true, req, res, next);
212 } else {
213 serveNormal(file, req, res, next);
214 }
215 });
216 } else {
217 serveNormal(file, req, res, next);
218 }
219 }
220
221 return serve;
222}

Callers 1

static.test.jsFile · 0.85

Calls 3

serveFileFromStatsFunction · 0.85
serveNormalFunction · 0.85
nextFunction · 0.50

Tested by

no test coverage detected