importexpressfrom"express"constserver=express()server.disable("x-powered-by")server.get("/hello",(request,response)=>{response.send("hello")})server.listen(8000,()=>{console.log("Server is listening")})
importexpressfrom"express"importcorsfrom"cors"constserver=express()constallowedOrigins=["yourdomain.com","anotherdomain.com"]server.use(cors({origin:(origin,next)=>{if(allowedOrigins.includes(origin)){next(null,true)}else{next(newError(`${origin} not allowed`))}}}))server.get("/hello",(request,response)=>{response.send("hello")})server.listen(8000,()=>{console.log("Server is listening")})
importexpressfrom"express"importhelmetfrom"helmet"constserver=express()server.use(helmet())server.get("/hello",(request,response)=>{response.send("hello")})server.listen(8000,()=>{console.log("Server is listening")})
importexpressfrom"express"importdotenvfrom"dotenv"dotenv.config()constconnect=(databaseCredentials)=>{console.log(databaseCredentials)// TODO: connect to database}constserver=express()server.get("/hello",(request,response)=>{constconnection=connect(process.env.DATABASE_CREDENTIALS)response.send("hello")})server.listen(8000,()=>{console.log("Server is listening")})
importexpressfrom"express"importsessionfrom"express-session"constserver=express()app.use(session({secret:process.env.SESSION_SECRET,resave:false,saveUninitialized:true,cookie:{secure:true,httpOnly:true,sameSite:true}}))server.get("/hello",(request,response)=>{response.send("hello")})server.listen(8000,()=>{console.log("Server is listening")})
importexpressfrom"express"importbodyParserfrom"body-parser"importhppfrom"hpp"constserver=express()server.use(bodyParser.urlencoded({extended:true}))server.use(hpp())server.get("/hello",(request,response)=>{response.json({name:request.query?.name??"unknown"})})server.listen(8000,()=>{console.log("Server is listening")})
importexpressfrom"express"importbodyParserfrom"body-parser"importzfrom"zod"constserver=express()server.use(bodyParser.json())server.post("/api/users",(request,response)=>{try{constschema=z.object({email:z.string(),password:z.string(),confirmation:z.string()})constinput=schema.parse(request.body)response.status(200).send("TODO: save the user")}catch(error){response.status(400).send(error.message)}})server.listen(8000,()=>{console.log("Server is listening")})
Looks like it is doing a lot more than just managing environment variables and I wanted to put a more lightweight alternative to dotenv in my article but this is not the goal and I wanted to use a battle-tested package to focus on the security side.
And I'm sure it will fit needs for those who are interested in this all-in-one configuration package.
Noted, no worries. As clarification, the advantage of this package is that works just as well in the browser, so you have one way to configure all your JavaScript. It now no longer matters if it is NodeJS or browser, it just works the same everywhere.
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (3)
Try wj-config for configuration instead of dotenv.
Hi José, thanks for sharing this package.
Looks like it is doing a lot more than just managing environment variables and I wanted to put a more lightweight alternative to
dotenv
in my article but this is not the goal and I wanted to use a battle-tested package to focus on the security side.And I'm sure it will fit needs for those who are interested in this all-in-one configuration package.
Noted, no worries. As clarification, the advantage of this package is that works just as well in the browser, so you have one way to configure all your JavaScript. It now no longer matters if it is NodeJS or browser, it just works the same everywhere.