mkdir simple-deploy-app-typescript-to-heroku
cd simple-deploy-app-typescript-to-heroku
npm init -y // automatic create new file package.json
npm i @types/express @types/node express nodemon ts-node typescript
- Express is used for making REST API easier, also setting up a server is a very pleasant developer experience compare to normal Nodejs methods.
- Nodemon keeps the server running and swapping the latest code so we don't need to restart the server every time our update new code.
- ts-node directly runs .ts node file.
- typescript for type-script support to javascript.
tsc --init // automatic for create new file tsconfig.json
Then add new line below compilerOptions
object.
"include" : [
"src/**/*.ts" /* Include every ts file in source folder */
],
"exclude" : [
"node_modules" /* exclude everything in node_modules */
]
Edit file package.json
"scripts": {
"start": "ts-node src/config/server.ts", //
"dev": "nodemon -x ts-node src/config/server.ts"
},
Server script would live in src/config/server.ts
Create new simple server with express now.
src/config/server.ts
import express from 'express';
const app = express()
const PORT : string|number = process.env.PORT || 5000;
app.use("*",(req, res) =>{
res.send("<h1>Welcome to your simple server! Awesome right</h1>");
});
app.listen(PORT,() => console.log(`hosting @${PORT}`));
Testing for server is running as well, we run cmd npm run dev
.
Substep 1: Installing Heroku CLI
heroku login
Then we are going to new windows browser for login to Heroku application.
Substep 3: Creating a heroku application in heroku
Add new line to file
web:ts-node/src/config/server.ts
git init .
git add .
git commit -m "Initializing project"
git push heroku master