Wednesday, September 4, 2024

Boilerplate NPM

 server.js

 /******************************************************
 * PLEASE DO NOT EDIT THIS FILE
 * the verification process may break
 * ***************************************************/

'use strict';

var fs = require('fs');
var express = require('express');
var app = express();

if (!process.env.DISABLE_XORIGIN) {
  app.use(function(req, res, next) {
    var allowedOrigins = ['https://narrow-plane.gomix.me', 'https://www.freecodecamp.com'];
    var origin = req.headers.origin || '*';
    if(!process.env.XORIG_RESTRICT || allowedOrigins.indexOf(origin) > -1){
         console.log(origin);
         res.setHeader('Access-Control-Allow-Origin', origin);
         res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    }
    next();
  });
}

app.use('/public', express.static(process.cwd() + '/public'));

app.route('/_api/package.json')
  .get(function(req, res, next) {
    console.log('requested');
    fs.readFile(__dirname + '/package.json', function(err, data) {
      if(err) return next(err);
      res.type('txt').send(data.toString());
    });
  });
 
app.route('/')
    .get(function(req, res) {
          res.sendFile(process.cwd() + '/views/index.html');
    })

// Respond not found to all the wrong routes
app.use(function(req, res, next){
  res.status(404);
  res.type('txt').send('Not found');
});

// Error Middleware
app.use(function(err, req, res, next) {
  if(err) {
    res.status(err.status || 500)
      .type('txt')
      .send(err.message || 'SERVER ERROR');
  }  
})

//Listen on port set in environment variable or default to 3000
const listener = app.listen(process.env.PORT || 3000, function () {
  console.log("Node.js listening on port " + listener.address().port);
});

package.json

{"author": "TCL",
    "description": "A boilerplate project",
    "keywords": [ "boilerplate", "nodejs", "test", "freecodecamp"],
    "license": "MIT",
    "version": "0.0.1",
    "name": "fcc-learn-npm-package-json",
    "dependencies": {"express": "^4.14.0"},
    "main": "server.js",
    "scripts": {
        "start": "node server.js"
    },
    "repository": {
        "type": "git",
        "url": "https://idontknow/todo.git"
    }
}

readme.md

# Backend Challenges boilerplate - package.json
[![Run on Repl.it](https://repl.it/badge/github/freeCodeCamp/boilerplate-npm)](https://repl.it/github/freeCodeCamp/boilerplate-npm)

views/index.html

<!DOCTYPE html>
<html>

   <head>
      <title>Backend Challenges | Free Code Camp</title>
      <link rel="shortcut icon" href="https://cdn.hyperdev.com/us-east-1%3A52a203ff-088b-420f-81be-45bf559d01b1%2Ffavicon.ico" type="image/x-icon"/>
      <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
      <link href="/public/style.css" rel="stylesheet" type="text/css">
   </head>

   <body>
      <div class="container">
         <h1>Manage Node.js projects and npm packages using package.json</h1>
         <h3>Part 1 of Free Code Camp Backend Challenges</h3>
      </div>
   </body>

</html>

public/style.css

/****** Main Styling ******/

body {
    font-family: 'Roboto', sans-serif;
    font-size: 16px;
    color: #222;
    background-color: #ECF0F1;
    text-align: center;
}

.container {
    padding: 0;
    margin-top: 40px;
}

.footer {
    margin-top: 60px;
}

ol {
    list-style-position: inside;
}
ul {
    list-style-type: none;
}

a {
    color: #2574A9;
}
/****** Logo Div Styling ******/

img {
    margin: 20px auto 0 auto;
    display: block;
}