Serve js files or json responses
I wanna do something like this with cloudflare workers:
How to do this, if possible
const express = require('express');
const fs = require('fs');
const path = require('path')
const bodyParser = require('body-parser');
const helmet = require('helmet');
const cors = require('cors'); // Add this line
const basicAuth = require('express-basic-auth');
const bcrypt = require('bcrypt');
const app = express();
const port = 3005;
const config = require('./config.json');
const mysql = require("mysql");
//Some DB connection, probably mongo
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'"],
styleSrc: ["'self'", "'unsafe-inline'"],
imgSrc: ["'self'", "data:"],
connectSrc: ["'self'"],
fontSrc: ["'self'"],
objectSrc: ["'none'"],
mediaSrc: ["'self'"],
frameSrc: ["'none'"],
},
}, dnsPrefetchControl: false, frameguard: {
action: 'deny'
}, hsts: {
maxAge: 5184000, // 60 days
includeSubDomains: true, preload: true,
}, ieNoOpen: true, noSniff: true, permittedCrossDomainPolicies: {
permittedPolicies: 'none',
}, referrerPolicy: {
policy: 'same-origin',
}, xssFilter: true,
}));
const corsOptions = {
origin: /.*somedomain.*/, credentials: true, exposedHeaders: 'Set-Cookie',
sameSite: 'Strict',
};
// Apply CORS middleware before body parsing
app.use(cors(corsOptions));
// Body parser with increased limits
app.use(bodyParser.json({limit: '1gb'}));
app.use(bodyParser.urlencoded({limit: '1gb', extended: true}));
app.use(express.static(path.join(__dirname, 'public')));
const excludedRoutes = [''];
// Basic authentication middleware
app.use((req, res, next) => {
// Check if the request path matches any of the excluded routes
if (excludedRoutes.includes(req.path)) {
// If the request path matches, skip basic authentication and move to the next middleware
next();
} else {
// If the request path doesn't match, proceed with basic authentication
basicAuth({
users,
challenge: true,
unauthorizedResponse: 'Unauthorized access',
})(req, res, next); // Call the basicAuth middleware
}
});
//part I actually want to implement
app.get('/get-data', (req,res)=>{
res.json('{"somedata":"some other data"}')
})
const express = require('express');
const fs = require('fs');
const path = require('path')
const bodyParser = require('body-parser');
const helmet = require('helmet');
const cors = require('cors'); // Add this line
const basicAuth = require('express-basic-auth');
const bcrypt = require('bcrypt');
const app = express();
const port = 3005;
const config = require('./config.json');
const mysql = require("mysql");
//Some DB connection, probably mongo
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'"],
styleSrc: ["'self'", "'unsafe-inline'"],
imgSrc: ["'self'", "data:"],
connectSrc: ["'self'"],
fontSrc: ["'self'"],
objectSrc: ["'none'"],
mediaSrc: ["'self'"],
frameSrc: ["'none'"],
},
}, dnsPrefetchControl: false, frameguard: {
action: 'deny'
}, hsts: {
maxAge: 5184000, // 60 days
includeSubDomains: true, preload: true,
}, ieNoOpen: true, noSniff: true, permittedCrossDomainPolicies: {
permittedPolicies: 'none',
}, referrerPolicy: {
policy: 'same-origin',
}, xssFilter: true,
}));
const corsOptions = {
origin: /.*somedomain.*/, credentials: true, exposedHeaders: 'Set-Cookie',
sameSite: 'Strict',
};
// Apply CORS middleware before body parsing
app.use(cors(corsOptions));
// Body parser with increased limits
app.use(bodyParser.json({limit: '1gb'}));
app.use(bodyParser.urlencoded({limit: '1gb', extended: true}));
app.use(express.static(path.join(__dirname, 'public')));
const excludedRoutes = [''];
// Basic authentication middleware
app.use((req, res, next) => {
// Check if the request path matches any of the excluded routes
if (excludedRoutes.includes(req.path)) {
// If the request path matches, skip basic authentication and move to the next middleware
next();
} else {
// If the request path doesn't match, proceed with basic authentication
basicAuth({
users,
challenge: true,
unauthorizedResponse: 'Unauthorized access',
})(req, res, next); // Call the basicAuth middleware
}
});
//part I actually want to implement
app.get('/get-data', (req,res)=>{
res.json('{"somedata":"some other data"}')
})
2 Replies
DNS over Discord: WHOIS
bonobobo.pt
-------------+------------------------------
Registration | Thu, 01 Aug 2024 09:47:37 GMT
bonobobo.pt
-------------+------------------------------
Registration | Thu, 01 Aug 2024 09:47:37 GMT
Thank you @nora
I have another question, can I upload R2 files with wrangler? for instance, when I deploy the app it will automatically upload files under a specific fodler
with the app.js inside wrangler
dunno if im making any sense