Skip to content

Commit

Permalink
moved entire code to express generator format
Browse files Browse the repository at this point in the history
  • Loading branch information
lostmypillow committed Aug 22, 2024
1 parent 6f604ee commit 086e7bc
Show file tree
Hide file tree
Showing 21 changed files with 4,002 additions and 3,418 deletions.
24 changes: 0 additions & 24 deletions .dockerignore

This file was deleted.

11 changes: 0 additions & 11 deletions Dockerfile

This file was deleted.

102 changes: 63 additions & 39 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,69 @@
const express = require("express");
const cors = require("cors");
const scrapeContent = require("./lib/scrapeContent");
const app = express();
const port = 3001;
const getHTMLFetch = require("./lib/getHTML")
app.use((req, res, next) => {
res.setHeader(
"Access-Control-Allow-Origin",
"https://lostmypillow.github.io/jDMS-web"
);
res.setHeader(
"Access-Control-Allow-Origin",
"http://localhost:5173"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS,CONNECT,TRACE"
);
res.setHeader(
"Access-Control-Allow-Headers",
"Content-Type, Authorization, X-Content-Type-Options, Accept, X-Requested-With, Origin, Access-Control-Request-Method, Access-Control-Request-Headers"
);
res.setHeader("Access-Control-Allow-Credentials", true);
res.setHeader("Access-Control-Allow-Private-Network", true);
// Firefox caps this at 24 hours (86400 seconds). Chromium (starting in v76) caps at 2 hours (7200 seconds). The default value is 5 seconds.
res.setHeader("Access-Control-Max-Age", 7200);

next();
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

// var indexRouter = require('./routes/index');
// var usersRouter = require('./routes/users');
var newsRouter = require('./routes/news');
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// app.use('/', indexRouter);
// app.use('/users', usersRouter);
app.use('/news', newsRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});

app.get("/test", async (req, res) => {
res.json(await scrapeContent(decodeURI(req.query.url)));
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

// app.get("/fetch", async (req, res) => {
// const v = await getHTMLFetch("https://www.techbang.com/posts/117423-idc-the-increasing-demand-for-ai-pc-and-ai-mobile-phone")
// console.log(v)
// res.json({message: "yo" });
// });


app.listen(port, () => {
console.log(`JDMS listening on port ${port}`);
});
app.use((req, res, next) => {
// res.setHeader(
// "Access-Control-Allow-Origin",
// "https://lostmypillow.github.io/jDMS-web"
// );
// res.setHeader(
// "Access-Control-Allow-Origin",
// "http://localhost:5173"
// );
// res.setHeader(
// "Access-Control-Allow-Methods",
// "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS,CONNECT,TRACE"
// );
// res.setHeader(
// "Access-Control-Allow-Headers",
// "Content-Type, Authorization, X-Content-Type-Options, Accept, X-Requested-With, Origin, Access-Control-Request-Method, Access-Control-Request-Headers"
// );
// res.setHeader("Access-Control-Allow-Credentials", true);
// res.setHeader("Access-Control-Allow-Private-Network", true);
// // Firefox caps this at 24 hours (86400 seconds). Chromium (starting in v76) caps at 2 hours (7200 seconds). The default value is 5 seconds.
// res.setHeader("Access-Control-Max-Age", 7200);

// next();
// });

module.exports = app;
90 changes: 90 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('planned-move:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
90 changes: 90 additions & 0 deletions bin/www_prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app-out');
var debug = require('debug')('planned-move:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
14 changes: 0 additions & 14 deletions docker-compose.debug.yml

This file was deleted.

12 changes: 0 additions & 12 deletions docker-compose.yml

This file was deleted.

29 changes: 26 additions & 3 deletions lib/getHTML.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
const fetch = require('node-fetch-commonjs');
const fetch = require("node-fetch-commonjs");
const puppeteer = require("puppeteer");

async function usingNodeFetch(link) {
return await (await fetch(link)).text();
}

async function usingPuppeteer(link) {
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
});
const page = await browser.newPage();
await page.goto(link, {
waitUntil: "domcontentloaded",
});

const html = await page.content();
await browser.close();
return html;
}

async function getHTML(link) {
return await (await fetch(link)).text()
return link.includes("ctee") || link.includes("chinatimes")
? await usingPuppeteer(link)
: await usingNodeFetch(link);
}

module.exports = getHTML
module.exports = getHTML;
Loading

0 comments on commit 086e7bc

Please sign in to comment.