0

I'm getting net::ERR_CONNECTION_REFUSED error when i open related page with javascript file. I'm using node.js as a server and , i'm writing post requests in a javascript file for my static pages. service_provider.js is my javascript file for i use writing javascript functions for my static pages(html). node.js is my node.js server file.

service_provider.js

function getJourneyAnalize(
    _fonkSuccess,
    _fonkBefore,
    _fonkError,
    _deviceId,
    _driverId,
    _dateStart,
    _dateEnd,
    _timeStart,
    _timeEnd,
    _map,
    _kmStart,
    _kmEnd
) {
    var _fromBody = {
        device_id: _deviceId,
        driver_id: _driverId,
        date_start: _dateStart,
        date_end: _dateEnd,
        time_start: _timeStart,
        time_end: _timeEnd,
        map: _map,
        km_start: _kmStart,
        km_end: _kmEnd
    };

    $.ajax({
        type: 'POST',
        url: apiUrl + '/ReportFms/JourneyAnalize',
        data: JSON.stringify(_fromBody),
        dataType: 'json',
        cache: false,
        contentType: 'application/json; charset=utf-8',
        beforeSend: function (xhr, settings) {
            _fonkBefore();
            xhr.setRequestHeader('Authorization', 'Bearer ' + currentUser.access_token);
        },
        success: _fonkSuccess,
        error: _fonkError
    });
}

node.js

const express = require('express');
const app = express();
var path = require('path');

app.use(
    function (req, res, next) {       
        res.setHeader('Access-Control-Allow-Origin', '*');        
        res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');       
        res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
        res.setHeader('Access-Control-Allow-Credentials', true);        
        next();
    }
)
  
app.use('/node_modules/jquery/dist', express.static(path.join(__dirname, '../node_modules/jquery/dist')))
    
app.get('/static-pages/journey-analize-report', function (req, res) {   
    res.sendFile(path.join(__dirname + '/../static-pages/journey-analize-report/index.html'))  
})
    
app.listen(8000, () => {
    console.log('server started');
})

1 Answers1

0

Does you static page shows correctly ?

1 - If your static page is in the parent folder, get rid of the first slash

res.sendFile(path.join(__dirname + '../static-pages/journey-analize-report/index.html'))

2 - to serve jquery script in your static file you could define a personnalized path in place of node_module folder

Replace:

app.use('/node_modules/jquery/dist', express.static(path.join(__dirname, '../node_modules/jquery/dist')))

With

app.use('/js', express.static(path.join(__dirname, '../node_modules/jquery/dist')))

and in your html file require jquery with personnalized path

<script src="/js/jquery.min.js"></script>

3 - if the error still exist, comment app.use functions and jquery script in static page. Reload the server to see if its work

Alex
  • 143
  • 7