Express example (#45)

This commit is contained in:
Jake Runzer
2021-02-02 23:45:59 -07:00
committed by GitHub
parent 3d13bd9ddd
commit 7eb78affe6
16 changed files with 2344 additions and 45 deletions

View File

@@ -19,3 +19,12 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local
# out dir
dist/

View File

@@ -1,18 +0,0 @@
FROM node:alpine
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
CMD [ "npm", "run", "start" ]

View File

@@ -3,7 +3,7 @@
This example starts an [ExpressJS](https://expressjs.com/) server that connects
to a Railway PostgreSQL database.
[![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/new?template=https%3A%2F%2Fgithub.com%2Frailwayapp%2Fexamples%2Ftree%2Fmaster%2Fexamples%2Fexpressjs-postgres)
[![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/new?template=https%3A%2F%2Fgithub.com%2Frailwayapp%2Fexamples%2Ftree%2Fmaster%2Fexamples%2Fexpressjs-postgres&plugins=postgresql)
## ✨ Features
@@ -12,6 +12,7 @@ to a Railway PostgreSQL database.
## 💁‍♀️ How to use
- Install dependencies `yarn`
- [Create a Railway project with the Postgres plugin](https://dev.new)
- Connect to your Railway project `railway init`
- Start the server `railway run yarn start`

View File

@@ -1,17 +1,26 @@
{
"name": "with-express",
"name": "expressjs-postgres",
"private": true,
"version": "1.0.0",
"main": "index.js",
"author": "Jake Runzer",
"license": "MIT",
"scripts": {
"start": "node src/index.js",
"build": "echo 'No build step'"
"dev": "nodemon src/index.ts",
"build": "tsc",
"start": "node dist/index.js"
},
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"pg": "^8.5.1"
},
"devDependencies": {}
"devDependencies": {
"@types/body-parser": "^1.19.0",
"@types/express": "^4.17.11",
"@types/pg": "^7.14.9",
"nodemon": "^2.0.7",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
}
}

View File

@@ -1,18 +0,0 @@
const express = require("express");
const pg = require("pg");
// Connect to the database using the DATABASE_URL environment
// variable injected by Railway
const pool = new pg.Pool();
const app = express();
const port = process.env.PORT || 3000;
app.get("/", async (req, res) => {
const { rows } = await pool.query("SELECT NOW()");
res.send(`Hello, World! ${rows[0].now}`);
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});

View File

@@ -0,0 +1,23 @@
import bodyParser from "body-parser";
import express from "express";
import pg from "pg";
// Connect to the database using the DATABASE_URL environment
// variable injected by Railway
const pool = new pg.Pool();
const app = express();
const port = process.env.PORT || 3333;
app.use(bodyParser.json());
app.use(bodyParser.raw({ type: "application/vnd.custom-type" }));
app.use(bodyParser.text({ type: "text/html" }));
app.get("/", async (req, res) => {
const { rows } = await pool.query("SELECT NOW()");
res.send(`Hello, World! The time from the DB is ${rows[0].now}`);
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});

View File

@@ -0,0 +1,13 @@
{
"compilerOptions": {
"outDir": "dist",
"incremental": true,
"target": "es6",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}

File diff suppressed because it is too large Load Diff