ipkn / crow

Crow is very fast and easy to use C++ micro web framework (inspired by Python Flask)
BSD 3-Clause "New" or "Revised" License
7.46k stars 889 forks source link

Html rendering as plain text #339

Closed asra-baig closed 5 years ago

asra-baig commented 5 years ago

Hello, I was able to view an html page as html when I would type it in main.cpp but after creating an index.html file and streaming it in, it is rendering as plain text on my browser.

This is my main.cpp:

#include "crow_all.h"
using namespace std;
using namespace crow;

int main(int argc, char* argv[]) {
    crow::SimpleApp app;

    CROW_ROUTE(app, "/")
        ([](const request &req, response &res){
            ifstream in("../public/index.html", ifstream::in);
            if(in) {
                ostringstream contents;
                contents << in.rdbuf();
                in.close();
                res.write(contents.str());
            } else {
                res.write("Not found");
            }
            res.end();
         });

    char* port = getenv("PORT");
    uint16_t iPort = static_cast<uint16_t>(port != NULL? stoi(port): 18080);
    cout << "PORT = " << iPort << "\n";
    app.port(iPort).multithreaded().run();
}

And this is my index.html: <!DOCKTYPE html>

Crow C++

H1, Crow.

asra-baig commented 5 years ago

Was able to view it as html by setting the response header to "text/html": set_header("Content-Type", "text/html");