-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
const envalid = require('../') | ||
|
||
module.exports = envalid.cleanEnv(process.env, { | ||
HOST: envalid.host({ default: '127.0.0.1' }), | ||
PORT: envalid.port({ default: 3000, desc: 'The port to start the server on' }), | ||
|
||
// For this example, the MESSAGE env var will be read from the .env | ||
// file in this directory (so the default value won't be used): | ||
MESSAGE: envalid.str({ default: 'Hello, world' }), | ||
HOST: envalid.host({ default: '127.0.0.1' }), | ||
PORT: envalid.port({ default: 3000, desc: 'The port to start the server on' }), | ||
MESSAGE: envalid.str({ default: 'Hello, world' }), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,30 @@ | ||
// Toy envalid example app. | ||
|
||
// This will start up an http server using the port and hostname specified in | ||
// any of three places. In descending order of preference, these are: | ||
// 1) The process environment | ||
// 2) A '.env' file | ||
// 3) Default values passed to envalid (via the 'default' or 'devDefault' properties.) | ||
// | ||
// This will start up an http server using the port and hostname specified in either: | ||
// 1) The process.env object, or | ||
// 2) Default values passed to envalid (via the 'default' or 'devDefault' properties.) | ||
// | ||
// From the root of the project, try the following commands and check out `env.js` to see how | ||
// Envalid works: | ||
// | ||
// node example/server.js | ||
// | ||
// PORT=1337 node example/server.js | ||
// | ||
// MESSAGE="hey there" node example/server.js | ||
// | ||
// NODE_ENV=development node example/server.js | ||
|
||
const http = require('http') | ||
const env = require('./env') | ||
|
||
const server = http.createServer((req, res) => { | ||
res.statusCode = 200 | ||
res.setHeader('Content-Type', 'text/plain') | ||
res.end(env.MESSAGE) | ||
res.statusCode = 200 | ||
res.setHeader('Content-Type', 'text/plain') | ||
res.end(env.MESSAGE) | ||
}) | ||
|
||
server.listen(env.PORT, env.HOST, () => { | ||
const serverType = env.isProduction ? 'production' : 'dev' | ||
console.log(`Server (${serverType}) running at http://${env.HOST}:${env.PORT}/`) | ||
const serverType = env.isProduction ? 'production' : 'dev' | ||
console.log(`Server (${serverType}) running at http://${env.HOST}:${env.PORT}/`) | ||
}) |