Skip to content
Snippets Groups Projects
Commit 8a42595c authored by AE6374's avatar AE6374
Browse files

Tehtävien 5 lisäys

parent 749f7ec4
No related branches found
No related tags found
No related merge requests found
console.log("Joona Pankkonen")
\ No newline at end of file
// demo.js
const userinfo = require('./user')
console.log(`${userinfo.getName()} lives in ${userinfo.getLocation()} and was born on ${userinfo.birthDate}`)
const fs = require('fs');
fs.readFile('numerot.txt', (err, data) => {
if (err) {
console.error('Error reading the file:', err);
return;
}
console.log('Reading file and calculate a sum...');
const numbers = data.toString().split(',').map(Number);
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log('Sum is', sum);
});
const http = require('http')
const fs = require ('fs')
const hostname = '127.0.0.1'
const port = 3000
const path = 'counter.txt'
const server = http.createServer((req, res) => {
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the file:', err)
res.statusCode = 500;
res.end('Server error');
return
}
let counter = parseInt(data, 10);
counter += 1;
fs.writeFile(path, counter.toString(), (err) => {
if (err) {
console.error('Error writing to the file:', err);
res.statusCode = 500;
res.end('Server error');
return;
}
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end(`Request counter value is ${counter}.`);
});
});
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`)
})
\ No newline at end of file
0
\ No newline at end of file
1,3,4,6,10,12
\ No newline at end of file
// calculator.js
const name = "Joona Pankkonen"
const location = "Jyväskylä"
const birthDate = "10.09.2000"
function getName(){
return name;
}
function getLocation() {
return location;
}
module.exports = {
getName, getLocation, birthDate
};
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment