Skip to content

Commit adb459b

Browse files
committed
New Express server added with access to LinkedIn API
1 parent 4994be3 commit adb459b

File tree

21 files changed

+4915
-0
lines changed

21 files changed

+4915
-0
lines changed

backend/.dockerignore

Whitespace-only changes.

backend/.env.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Postgres DB
2+
DATABASE_SCHEMA=postgres
3+
DATABASE_USERNAME=postgres
4+
DATABASE_PASSWORD=postgres
5+
DATABASE_HOST=localhost
6+
DATABASE_PORT=5432
7+
8+
# API
9+
HOST_PROTOCOL=http://
10+
HOST_PORT=3000
11+
HOST_API="${HOST_PROTOCOL}://localhost:${HOST_PORT}"

backend/.gitignore

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# Compiled output
4+
/dist
5+
/tmp
6+
/out-tsc
7+
/bazel-out
8+
9+
# Node
10+
/node_modules
11+
npm-debug.log
12+
yarn-error.log
13+
.env
14+
15+
# IDEs and editors
16+
.idea/
17+
.project
18+
.classpath
19+
.c9/
20+
*.launch
21+
.settings/
22+
*.sublime-workspace
23+
24+
# Visual Studio Code
25+
.vscode/*
26+
!.vscode/settings.json
27+
!.vscode/tasks.json
28+
!.vscode/launch.json
29+
!.vscode/extensions.json
30+
.history/*
31+
32+
# Miscellaneous
33+
/.angular/cache
34+
.sass-cache/
35+
/connect.lock
36+
/coverage
37+
/libpeerconnection.log
38+
testem.log
39+
/typings
40+
41+
# System files
42+
.DS_Store
43+
Thumbs.db

backend/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM node:18.16.0-alpine3.17
2+
RUN mkdir -p /src
3+
COPY package.json /src/package.json
4+
WORKDIR /src
5+
RUN npm i --only=production --silent
6+
COPY . /src
7+
EXPOSE 3000
8+
CMD npm start

backend/app.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const express = require("express");
2+
const path = require("path");
3+
const cookieParser = require("cookie-parser");
4+
const logger = require("morgan");
5+
require("dotenv").config();
6+
7+
const indexRouter = require("./routes/index");
8+
const usersRouter = require("./routes/users");
9+
10+
const app = express();
11+
12+
app.use(logger("dev"));
13+
app.use(express.json());
14+
app.use(express.urlencoded({ extended: false }));
15+
app.use(cookieParser());
16+
app.use(express.static(path.join(__dirname, "public")));
17+
18+
app.use("/", indexRouter);
19+
app.use("/users", usersRouter);
20+
21+
module.exports = app;

backend/bin/migrate.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const db = require("../models/database");
2+
db.sequelize.sync();

backend/bin/www

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var app = require("../app");
8+
var debug = require("debug")("katacoda-nodejs-express-example:server");
9+
var http = require("http");
10+
11+
/**
12+
* Get port from environment and store in Express.
13+
*/
14+
15+
var port = normalizePort(process.env.PORT || "3000");
16+
app.set("port", port);
17+
18+
/**
19+
* Create HTTP server.
20+
*/
21+
22+
var server = http.createServer(app);
23+
24+
/**
25+
* Listen on provided port, on all network interfaces.
26+
*/
27+
28+
server.listen(port);
29+
server.on("error", onError);
30+
server.on("listening", onListening);
31+
32+
/**
33+
* Normalize a port into a number, string, or false.
34+
*/
35+
36+
function normalizePort(val) {
37+
var port = parseInt(val, 10);
38+
39+
if (isNaN(port)) {
40+
// named pipe
41+
return val;
42+
}
43+
44+
if (port >= 0) {
45+
// port number
46+
return port;
47+
}
48+
49+
return false;
50+
}
51+
52+
/**
53+
* Event listener for HTTP server "error" event.
54+
*/
55+
56+
function onError(error) {
57+
if (error.syscall !== "listen") {
58+
throw error;
59+
}
60+
61+
var bind = typeof port === "string" ? "Pipe " + port : "Port " + port;
62+
63+
// handle specific listen errors with friendly messages
64+
switch (error.code) {
65+
case "EACCES":
66+
console.error(bind + " requires elevated privileges");
67+
process.exit(1);
68+
break;
69+
case "EADDRINUSE":
70+
console.error(bind + " is already in use");
71+
process.exit(1);
72+
break;
73+
default:
74+
throw error;
75+
}
76+
}
77+
78+
/**
79+
* Event listener for HTTP server "listening" event.
80+
*/
81+
82+
function onListening() {
83+
var addr = server.address();
84+
var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
85+
debug("Listening on " + bind);
86+
}

backend/config/db.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
host: process.env.DATABASE_HOST || "localhost",
3+
port: process.env.DATABASE_PORT || 5432,
4+
dialect: "postgres",
5+
dialectOptions: {
6+
ssl: process.env.DATABASE_SSL === "true",
7+
},
8+
9+
define: {
10+
timestamps: false,
11+
},
12+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
module.exports = {
4+
host: process.env.DATABASE_HOST || "localhost",
5+
port: process.env.DATABASE_PORT || 5432,
6+
dialect: "postgres",
7+
dialectOptions: {
8+
ssl: process.env.DATABASE_SSL === "true"
9+
},
10+
define: {
11+
timestamps: false
12+
}
13+
};

backend/models/database.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const dbConfig = require("../config/db.config");
2+
const Sequelize = require("sequelize");
3+
const sequelize = new Sequelize(process.env.DATABASE_SCHEMA || "postgres", process.env.DATABASE_USERNAME || "postgres", process.env.DATABASE_PASSWORD || "postgres", {
4+
host: dbConfig.host,
5+
port: dbConfig.port,
6+
dialect: dbConfig.dialect,
7+
dialectOptions: {
8+
ssl: dbConfig.dialectOptions.ssl,
9+
},
10+
define: {
11+
timestamps: dbConfig.define.timestamps,
12+
},
13+
});
14+
15+
const db = {};
16+
17+
db.Sequelize = Sequelize;
18+
db.sequelize = sequelize;
19+
20+
/* Define user table */
21+
db.User = require("./user.model")(sequelize, Sequelize);
22+
23+
module.exports = db;

0 commit comments

Comments
 (0)