Home > Software design >  Error: Not Found The requested URL was not found on this server
Error: Not Found The requested URL was not found on this server

Time:09-02

I'm trying to send some html form data to a node js file using express. However as I submit the form I keep getting this error on the browser (I've tried different browsers and restarted the server every time):

enter image description here

Here's the code:

import mysql from 'mysql2';
import express from 'express';
import path from 'path';


let probes = [];
let measurements = [];
let to_country;

const app = express();
const __dirname = path.resolve();
app.use(express.urlencoded( {extended: true} ));
app.use(express.static(__dirname));

const con = mysql.createConnection({
    host:'localhost',
    user:'root',
    password:'',
    database:'probes&anchors'
});

con.connect((err)=>{
    if(err){
        console.log("Connection not proper");
    }else{
        console.log("connected");
    }
});

app.post('/', async function(req,res){

    to_country = req.body.to_country;
    let sql = 'SELECT id FROM probes WHERE country=?';
    await con.promise().query(sql, [req.body.from_country], (err, rows) => {
        if (err) throw err;
        
        probes.push(rows.id);
        console.log('Probe id: ', rows.id);
    })
    console.log(probes);
    con.end();

    res.send("Loaded");
});

app.get('/', (req, res) => res.send('Hello World! From Node.js'));
app.listen(8000, () => console.log('Example app listening on port 8000!'));
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fetch API Demo</title>
</head>
<body>
    <form action="/" method="POST">
        <label for="from_country">From:</label>
        <select id="from_country" name="from_country">
            <option>Country1</option>
            <option value="AF">Afghanistan</option>
            <option value="AX">Aland Islands</option>
        </select>

        <label for="to_country">To:</label>
        <select id="to_country" name="to_country">
            <option>Country2</option>
            <option value="AF">Afghanistan</option>
            
        </select>
        <input type="submit" value="Latency?">
    </form> 
    
</body>
</html>

Both files are in the same directory.
I've modified Apache httpd.conf uncommenting two lines and adding the following line as shown here:

ProxyPass / http://localhost:8000

Can someone please help out? Any help is appreciated, thanks.

CodePudding user response:

First you could clean up your REST calls by using a REST package such as Axios.

`import mysql from 'mysql';
import mysql from 'mysql';
import express from 'express';
import {promisify} from 'util';
import path from 'path';
import axios from 'axios';

let probes = [];
let measurements = [];
let to_country;
const port = 80;

const app = express();
app.use(express.urlencoded( {extended: false} ));


function getTimestampInSeconds () {

    const date = Data.now();
    date.setMonth(date.getMonth() - 1);
    console.log(date);
    return Math.floor(date / 1000)
}

const con = mysql.createConnection({
    host:'localhost',
    user:'root',
    password:'',
   database:'probes&anchors'
});

con.connect((err)=>{
    if(err){
        console.log("Connection not proper");
    }else{
        console.log("connected");
    }
});

function fetchData(toCountry) {
    axios.get("https://atlas.ripe.net/api/v2/anchor-measurements/? include=target").then(response => {
         const data = await response.json();
        for (let anchor_measure of data.results){
             /* code for status 200 */
        }
        .catch(error => {
             /* code for any error (non-200) */
        })
    })
}`

CodePudding user response:

As I can see you are running apache server and node.js server on same port which requires some special configurations

you have to tell apache server explicitly to send (all or in some cases request coming from specific path) requests to node.js server (this technique is called reverse proxy technique)

this blog below might help you understand that here or here

  • Related