Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added code for creating robots.txt #838

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ This will install `http-server` globally so that it may be run from the command
|`-S`, `--tls` or `--ssl` |Enable secure request serving with TLS/SSL (HTTPS)|`false`|
|`-C` or `--cert` |Path to ssl cert file |`cert.pem` |
|`-K` or `--key` |Path to ssl key file |`key.pem` |
|`-r` or `--robots` | Automatically provide a /robots.txt (The content of which defaults to `User-agent: *\nDisallow: /`) | `false` |
|`-r` or `--robots` | Automatically creates a /robots.txt file (The content of which defaults to `User-agent: *\nDisallow: /`) | `false` |
|`--no-dotfiles` |Do not show dotfiles| |
|`--mimetypes` |Path to a .types file for custom mimetype definition| |
|`-h` or `--help` |Print this list and exit. | |
Expand Down
2 changes: 1 addition & 1 deletion bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ if (argv.h || argv.help) {
' -C --cert Path to TLS cert file (default: cert.pem)',
' -K --key Path to TLS key file (default: key.pem)',
'',
' -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]',
' -r --robots Creates a /robots.txt file [User-agent: *\\nDisallow: /]',
' --no-dotfiles Do not show dotfiles',
' --mimetypes Path to a .types file for custom mimetype definition',
' -h --help Print this list and exit.',
Expand Down
4 changes: 2 additions & 2 deletions doc/http-server.1
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ Passphrase will be read from NODE_HTTP_SERVER_SSL_PASSPHRASE (if set)

.TP
.BI \-r ", " \-\-robots " " [\fIUSER\-AGENT\fR]
Respond to /robots.txt request.
If not specified, uses "User-agent: *\\nDisallow: /]"
Creates a /robots.txt file.
Defaults to ["User-agent: *\\nDisallow: /]"

.TP
.BI \-\-no\-dotfiles
Expand Down
27 changes: 17 additions & 10 deletions lib/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,25 @@ function HttpServer(options) {
} : null));
}

if (options.robots) {
before.push(function (req, res) {
if (req.url === '/robots.txt') {
res.setHeader('Content-Type', 'text/plain');
var robots = options.robots === true
? 'User-agent: *\nDisallow: /'
: options.robots.replace(/\\n/, '\n');
// if (options.robots) {
// before.push(function (req, res) {
// if (req.url === '/robots.txt') {
// res.setHeader('Content-Type', 'text/plain');
// var robots = options.robots === true
// ? 'User-agent: *\nDisallow: /'
// : options.robots.replace(/\\n/, '\n');

return res.end(robots);
}
// return res.end(robots);
// }

res.emit('next');
// res.emit('next');
// });
// }

if (options.robots) {
fs.appendFile(`${this.root}/robots.txt`, 'User-agent: *\nDisallow: /', function (err) {
if (err) throw err;
// console.log('robots.txt created!');
});
}

Expand Down