Quantcast
Channel: Active questions tagged https - Stack Overflow
Viewing all articles
Browse latest Browse all 1818

How to host a free Node.js HTTP → HTTPS proxy

$
0
0

I have some IoT devices (SIM800L modules) that can only send HTTP POST requests, but my Next.js API is hosted on Vercel and only accepts HTTPS.

I wrote a simple Node.js proxy that listens for HTTP POST and forwards the data to my Next.js API over HTTPS.

I’m looking for a free hosting solution that allows HTTP POST requests to reach this Node.js server.

I’ve looked at Heroku, Render, Glitch, and Railway, but some of them either require a credit card or I’m not sure they will allow plain HTTP.

What free hosting options exist where I can run this kind of HTTP → HTTPS proxy?

Here is the route.ts file in my next.js HTTPS website:

import { NextResponse } from 'next/server';import { database } from '@/app/lib/firebaseconfig';import { ref, push, serverTimestamp } from 'firebase/database';// Validate API key from environment variableconst validateApiKey = (apiKey: string | null) => {    const validApiKey = process.env.API_KEY;    if (!validApiKey) {        console.error('API_KEY not configured in environment variables');        return false;    }    return apiKey === validApiKey;};export async function POST(request: Request) { try {// Check for API key in headersconst apiKey = request.headers.get('x-api-key');if (!validateApiKey(apiKey)) {  return NextResponse.json(    { error: 'Unauthorized' },    { status: 401 }  );}// Parse the incoming requestconst data = await request.json();// Convert moisture to number if it's a stringconst moisture = Number(data.moisture);// Validate the request dataif (!data.trailId || isNaN(moisture)) {  console.log('Validation failed:', { trailId: data.trailId, moisture });  return NextResponse.json(    { error: 'Invalid data format' },    { status: 400 }  );}// Reference to the specific trail's readingsconst readingsRef = ref(database, `${data.trailId}-readings`);// Add new reading to Firebaseawait push(readingsRef, {  moisture: moisture,  timestamp: serverTimestamp()});return NextResponse.json(  { success: true, moisture },  { status: 200 });} catch (error) {console.error('Error processing reading:', error);return NextResponse.json(  { error: 'Internal server error' },  { status: 500 });}}

And here is the code from my node.js server, that would listen to a post request on http and then perform a https post to my website

const express = require('express');const axios = require('axios');const app = express();app.use(express.json());app.post("/proxy", async (req, res) => {   try {console.log("Incoming data:", req.body);const response = await axios.post("https://live-trail-server.vercel.app/api/data",  {    trailId: req.body.trailId,    moisture: req.body.moisture,  },  {    headers: {"Content-Type": "application/json","x-api-key": req.body.apiKey,    },  });res.json(response.data);} catch (err) {   console.error("Proxy fetch error:", err.message);   res.status(500).json({ error: "Proxy error", details: err.message });} });app.listen(3000, () => {  console.log("Proxy server running on http://localhost:3000");});

Thanks!


Viewing all articles
Browse latest Browse all 1818

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>