Express: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
(Created page with "<source lang="bash"> npm adduser --registry=https://cdn.chorke.org/nexus/repository/npm/ : <<'EOF' npm notice Log in on https://cdn.chorke.org/nexus/repository/npm/ Username:...")
 
 
(8 intermediate revisions by the same user not shown)
Line 8: Line 8:
Logged in as academia on https://cdn.chorke.org/nexus/repository/npm/.
Logged in as academia on https://cdn.chorke.org/nexus/repository/npm/.
EOF
EOF
</source>


==JavaScript==
<source lang="bash">
mkdir -p ~/Documents/express_ws/express-micro-service/
mkdir -p ~/Documents/express_ws/express-micro-service/
cd ~/Documents/express_ws/express-micro-service/
cd ~/Documents/express_ws/express-micro-service/
npm init --yes
npm init --yes
vim package.json
vim package.json


cat <<-'EOF' >> ./index.js
cat <<-'EOF' >> ./index.js
const express = require('express');
const express = require('express');
const app = express();
const app = express();
const PORT = 8000;
const PORT = 2013;
app.get('/', (req, res) => res.send('Express + TypeScript Server'));
app.get('/', (req, res) => res.send('Express + TypeScript Server'));
app.listen(PORT, () => {
app.listen(PORT, () => {
Line 25: Line 29:


node index.js
node index.js
</source>
http://localhost:2013/
==TypeScript==
<source lang="bash">
yarn add -D typescript ts-node
cat <<-'EOF' >> ./tsconfig.json
{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "rootDir": "./",
    "outDir": "./build",
    "esModuleInterop": true,
    "strict": true
  }
}
EOF
yarn add -D @types/node @types/express
</source>
</source>


http://localhost:8000/
<source lang="bash">
rm -rf index.js
cat <<-'EOF' >> ./index.ts
import express from 'express';


const app = express();
const PORT = 2013;
app.get('/', (req, res) => res.send('Express + TypeScript Server'));
app.listen(PORT, () => {
  console.log(`[server]: Server is running at https://localhost:${PORT}`);
});
EOF
</source>
<source lang="bash">
yarn add -D nodemon
: <<'EOF'
"scripts": {
  "start": "nodemon index.ts"
}
EOF
</source>
<source lang="bash">
yarn start
: <<'EOF'
yarn run v1.22.10
warning ../../../package.json: No license field
$ nodemon index.ts
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node index.ts`
[server]: Server is running at https://localhost:2013
EOF
</source>
http://localhost:2013/
==Compile==
<source lang="bash">
: <<'EOF'
"scripts": {
  "start": "nodemon index.ts",
  "build": "tsc --project ./"
}
EOF
yarn run build
: <<'EOF'
yarn run v1.22.10
warning ../../../package.json: No license field
$ tsc --project ./
✨  Done in 3.88s.
EOF
</source>


==References==
==References==
* [https://blog.logrocket.com/typescript-with-node-js-and-express/ Using TypeScript with Node.js and Express]
* [https://blog.logrocket.com/typescript-with-node-js-and-express/ Using TypeScript with Node.js and Express]
* [https://stackoverflow.com/questions/31645341/ suppress variable substitution in bash]
* [https://stackoverflow.com/questions/31645341/ Suppress Variable Substitution in Bash]
* [https://github.com/w3tecch/express-typescript-boilerplate Express Typescript Boilerplate]
* [https://www.typescriptlang.org/docs/ TypeScript Documentation]
* [https://medium.com/programming-sage/handlebars-in-node-js-tutorial-a30a41fc6206 Handlebars and Node.js]
* [https://help.sonatype.com/repomanager3/formats/npm-registry Nexus3 NPM Registry]
* [https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types Definitely Typed]
* [https://github.com/typeorm/typeorm TypeORM]

Latest revision as of 06:25, 7 February 2021

npm adduser --registry=https://cdn.chorke.org/nexus/repository/npm/
: <<'EOF'
npm notice Log in on https://cdn.chorke.org/nexus/repository/npm/
Username: (academia) 
Password: (<default hidden>) 
Email: (this IS public) ([email protected]) 
Logged in as academia on https://cdn.chorke.org/nexus/repository/npm/.
EOF

JavaScript

mkdir -p ~/Documents/express_ws/express-micro-service/
cd ~/Documents/express_ws/express-micro-service/
npm init --yes
vim package.json
yarn add [email protected]

cat <<-'EOF' >> ./index.js
const express = require('express');
const app = express();
const PORT = 2013;
app.get('/', (req, res) => res.send('Express + TypeScript Server'));
app.listen(PORT, () => {
  console.log(`[server]: Server is running at http://localhost:${PORT}`);
});
EOF

node index.js
http://localhost:2013/

TypeScript

yarn add -D typescript ts-node
cat <<-'EOF' >> ./tsconfig.json
{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "rootDir": "./",
    "outDir": "./build",
    "esModuleInterop": true,
    "strict": true
  }
}
EOF
yarn add -D @types/node @types/express
rm -rf index.js 
cat <<-'EOF' >> ./index.ts
import express from 'express';

const app = express();
const PORT = 2013;
app.get('/', (req, res) => res.send('Express + TypeScript Server'));
app.listen(PORT, () => {
  console.log(`[server]: Server is running at https://localhost:${PORT}`);
});
EOF
yarn add -D nodemon
: <<'EOF'
"scripts": {
  "start": "nodemon index.ts"
}
EOF
yarn start
: <<'EOF'
yarn run v1.22.10
warning ../../../package.json: No license field
$ nodemon index.ts
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node index.ts`
[server]: Server is running at https://localhost:2013
EOF
http://localhost:2013/

Compile

: <<'EOF'
"scripts": {
  "start": "nodemon index.ts",
  "build": "tsc --project ./"
}
EOF

yarn run build
: <<'EOF'
yarn run v1.22.10
warning ../../../package.json: No license field
$ tsc --project ./
✨  Done in 3.88s.
EOF

References