Advertisement
okpalan

custom-mime-type.js

Nov 26th, 2023 (edited)
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.30 KB | Source Code | 0 0
  1. const http = require('http');
  2. const fs = require('fs');
  3. const path = require('path');
  4.  
  5. const server = http.createServer((req, res) => {
  6.   const filePath = path.join(__dirname, req.url);
  7.   const ext = path.extname(filePath);
  8.  
  9.   if (ext === '.custom-file-type') {
  10.     fs.readFile(filePath, (err, data) => {
  11.       if (err) {
  12.         res.writeHead(404, { 'Content-Type': 'text/plain' });
  13.         res.end('File not found');
  14.       } else {
  15.         res.writeHead(200, { 'Content-Type': 'custom/filetype' });
  16.         res.end(data);
  17.       }
  18.     });
  19.   } else {
  20.     // Handle other file types or routes
  21.   }
  22. });
  23.  
  24. server.listen(3000, () => {
  25.   console.log('Server listening on port 3000');
  26. });
  27.  
  28. const express = require('express');
  29. const fs = require('fs');
  30. const path = require('path');
  31.  
  32. const app = express();
  33.  
  34. app.use((req, res, next) => {
  35.   const filePath = path.join(__dirname, req.url);
  36.   const ext = path.extname(filePath);
  37.  
  38.   if (ext === '.testext') {
  39.     fs.readFile(filePath, (err, data) => {
  40.       if (err) {
  41.         res.status(404).type('text/plain').send('File not found');
  42.       } else {
  43.         res.status(200).type('your-custom-mime-type').send(data);
  44.       }
  45.     });
  46.   } else {
  47.     next();
  48.   }
  49. });
  50.  
  51. app.listen(3000, () => {
  52.   console.log('Server listening on port 3000');
  53. });
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement