Getting started Node.js
curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install nodejs
sudo apt-get install build-essential
sudo apt-get install npm
Create a file "app.js"
// This application uses express as web server, see: http://expressjs.com
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(function(req, res, next) {
res.set("Access-Control-Allow-Methods","POST, GET, DELETE, PUT");
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var fs = require("fs");
var path = require("path");
// Make files in ./public accessible
app.use(express.static(path.join(__dirname, '/src')));
// start server on the specified port
app.listen(process.env.PORT || 80, '0.0.0.0', function() {
console.log("server started");
});
app.get('/',function (req, res) {
fs.readFile( "index.html", 'utf8', function (err, data) {
res.end( data );
});
});
Create a file "package.json"
{
"name": "testhtml",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js",
"test": "mocha"
},
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.16.4",
"pg": "^7.10.0"
},
"repository": {},
"engines": {
"node": "^10.15.3"
}
}
Create a folder "public" and add the index.html
"npm install"
"npm start" or "node app.js"
sudo npm install -g express-generator
sudo npm install -g nodemon
npm install -g mocha
nodemon bin/www
- How to add more dependencies?
- How to setup and use tests?
- Setup IDE?
- How to run tests on push?
https://www.npmjs.com/package/express