headerparser/index.js

35 lines
1021 B
JavaScript
Raw Permalink Normal View History

// index.js
2017-02-18 19:55:28 +01:00
// where your node app starts
// init project
2023-03-27 17:57:53 +02:00
require("dotenv").config();
var express = require("express");
2017-02-18 19:55:28 +01:00
var app = express();
// enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
// so that your API is remotely testable by FCC
2023-03-27 17:57:53 +02:00
var cors = require("cors");
app.use(cors({ optionsSuccessStatus: 200 })); // some legacy browsers choke on 204
2017-02-18 19:55:28 +01:00
// http://expressjs.com/en/starter/static-files.html
2023-03-27 17:57:53 +02:00
app.use(express.static("public"));
2017-02-18 19:55:28 +01:00
// http://expressjs.com/en/starter/basic-routing.html
2023-03-27 17:57:53 +02:00
app.get("/", function (req, res) {
res.sendFile(__dirname + "/views/index.html");
2017-02-18 19:55:28 +01:00
});
// your first API endpoint...
2023-03-27 18:24:38 +02:00
app.get("/api/whoami", (req, res) => {
res.json({
ipaddress: req.headers["cf-connecting-ip"] || req.ip,
language: req.headers["accept-language"],
software: req.headers["user-agent"],
});
2017-02-18 19:55:28 +01:00
});
// listen for requests :)
var listener = app.listen(process.env.PORT || 3000, function () {
2023-03-27 17:57:53 +02:00
console.log("Your app is listening on port " + listener.address().port);
2017-02-18 19:55:28 +01:00
});