简体   繁体   中英

Send http request in nodejs to php file running on apache server

I'm gonna build my own real-time chat room therefore I need to use nodejs as a server.
After reading document in socket.io, I decide to use express.js also.
But before using nodejs, I have been create some html, php, javascript files on xampp.
I want to use those existing file so I can bulid without repeating.

In login part, I want to post data to login.php file to verify therefore I use http request to do this stuff.

apache is running on localhost port default port(80) and 8080
nodejs is running on localhost port 3000

there's my login.js used in router

const express = require('express');
const path = require('path');
const router = express.Router();
const bodyParser = require('body-parser');

const http = require('http');

const options = {
    hostname: 'localhost',
    path: 'project/php/login.php',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
    }
};

/* GET login page. */
router.get('/', function (req, res, next) {
    res.sendFile(path.join(__dirname, '..', 'views', 'login.html'));
});

/* POST login data */
router.post('/', function (req, res, next) {
    const username = req.body.username;
    const password = req.body.password;

    const authReq = http.request(options, (authRes) => {
        console.log(`Status: ${authRes.statusCode}`);
        console.log(`Headers: ${JSON.stringify(authRes.headers)}`);
        authRes.setEncoding('utf8');
        authRes.on('data', (chunk) => {
            console.log(`Body: ${chunk}`);
        });
        authRes.on('end', () => {
            console.log('No more data in response.');
        });
    });

    authReq.on('error', (e) => {
        console.error(`Problem with request: ${e.message}`);
    });
    
    // Write the data to be posted to the request body
    authReq.write(`username=${username}&password=${password}`);
    authReq.end();
});

module.exports = router;

However, I got this response

POST /login - - ms - -
Status: 400
Headers: {"date":"Mon, 09 Jan 2023 12:48:47 GMT","server":"Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/7.4.29","content-length":"334","connection":"close","content-type":"text/html; charset=iso-8859-1"}
Body: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/7.4.29 Server at domain Port 80</address>
</body></html>

Is there something wrong? Any help is appreciated.

For someone who did the foolish like me.

This went wrong because of the 'path' in options
path: 'project/php/login.php' should be changed to '/project/php/login.php'

Everything works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM