Skip to content
Snippets Groups Projects
Commit d7ea995d authored by Dmitriy Safronov's avatar Dmitriy Safronov
Browse files

initial

parent adfc0e63
No related branches found
No related tags found
No related merge requests found
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/javascript-node
{
"name": "Node.js",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/javascript-node:0-18",
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
"settings": {},
"extensions": [
"dbaeumer.vscode-eslint"
]
}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'portsAttributes' to set default properties for specific forwarded ports.
"portsAttributes": {
"3000": {
"label": "Hello Remote World",
"onAutoForward": "notify"
}
},
// Use 'otherPortsAttributes' to configure any ports that aren't configured using 'portsAttributes'.
// "otherPortsAttributes": {
// "onAutoForward": "silent"
// },
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "npm install"
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
# Exclude all...
*
# ...except essential
!.eslintrc.json
!index.js
!package*.json
{
"root": true,
"env": {
"node": true,
"es6": true
},
"rules": {
"no-console": 0,
"eqeqeq":"warn",
"no-cond-assign": 0,
"no-unused-vars": 1,
"no-extra-semi": "warn",
"semi": "warn"
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaFeatures": {
"experimentalObjectRestSpread": true
}
}
}
/node_modules
ignored:
- DL3008
{
"configurations": [
{
"name": "Launch Program",
"program": "${workspaceFolder}/index.js",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
}
]
}
\ No newline at end of file
# --------------> The build image
FROM node:18.12.1 AS build
RUN apt-get update && apt-get install -y --no-install-recommends dumb-init
ARG NPM_TOKEN
WORKDIR /usr/src/app
COPY package*.json /usr/src/app/
RUN echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc && \
npm ci --only=production && \
rm -f .npmrc
# --------------> The production image
FROM node:18.12.1-slim
ENV NODE_ENV production
COPY --from=build /usr/bin/dumb-init /usr/bin/dumb-init
USER node
WORKDIR /usr/src/app
COPY --chown=node:node --from=build /usr/src/app/node_modules /usr/src/app/node_modules
COPY --chown=node:node . /usr/src/app
CMD ["dumb-init", "node", "index.js"]
index.js 0 → 100644
var express = require("express");
var app = express();
// Ingress
app.enable('trust proxy');
// --- <Logger
const winston = require('winston');
const consoleTransport = new winston.transports.Console();
const myWinstonOptions = {
transports: [consoleTransport]
};
const logger = new winston.createLogger(myWinstonOptions);
function logRequest(req, res, next) {
logger.info('ip: ' + req.ip + ', hostname: ' + req.hostname + ', url: ' + req.url);
next();
}
app.use(logRequest);
function logError(err, req, res, next) {
logger.error('ip: ' + req.ip + ', hostname: ' + req.hostname + ', url: ' + req.url + ' <-> ' + err);
next();
}
app.use(logError);
// --- Logger>
app.get("/", (req, res, next) => {
res.status(200).send('<a href="/api">/api</a>');
next();
});
app.get("/api", (req, res, next) => {
res.status(200).send('<a href="/api/users">/api/users</a>');
next();
});
app.get("/api/users", (req, res, next) => {
res.json([
{"id": "1", "title": "One"},
{"id": "2", "title": "Two"},
{"id": "3", "title": "Three"},
{"id": "4", "title": "Four"}
]);
next();
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});
This diff is collapsed.
{
"dependencies": {
"express": "^4.18.2",
"winston": "^3.8.2"
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment