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

Node express #45

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/React-Todo-list-main/.pnp
/React-Todo-list-main/package-lock.json


# testing
/coverage

Expand All @@ -22,3 +21,8 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# express app
node-express/.env
node-express/node_modules

56 changes: 56 additions & 0 deletions node-express/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require('dotenv').config();
const statsig = require('statsig-node');
const express = require('express');
const { v4: uuidv4 } = require('uuid');
const { engine } = require('express-handlebars');
const cookieParser = require('cookie-parser');
const app = express();
const port = 3000;

app.use(cookieParser());
app.engine('.hbs', engine({ extname: '.hbs' }));
app.set('view engine', '.hbs');
app.set('views', './views');

/**
* global middleware to set a cookie if it doesn't exist
* passes the uuid down to route handlers on req object
*/
app.use(async (req, res, next) => {
let statsigDeviceID;
if (req.cookies['statsig_uuid']) {
statsigDeviceID = req.cookies['statsig_uuid'];
}
else {
statsigDeviceID = uuidv4();
res.cookie('statsig_uuid', statsigDeviceID);
}
req.statsigDeviceID = statsigDeviceID;
next();
})

app.get('/', async (req, res) => {
const user = {
// use the random uuid in cookie as the userID
// in practice, you'd use a known userID here
userID: req.statsigDeviceID,
country: 'US',
custom: {
employee: true
}
};
const testGroup = statsig.getExperimentSync(user, 'express_app_test').get('variant', 'control');
const backgroundFeatureEnabled = statsig.checkGate(user, 'express_app_bg');
res.render('index.hbs', {
testGroup,
backgroundFeatureEnabled
});
});

app.listen(port, async () => {
const runtimeEnv = process.env.STATSIG_ENV_TIER || 'production';
await statsig.initialize(process.env.STATSIG_SERVER_KEY, {
environment: { tier: runtimeEnv },
});
console.log(`Example app listening in ${runtimeEnv} on port ${port}`);
})
Binary file added node-express/docs/app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading