class AutoSaveJSON {
constructor(path) {
this.data = require(path);
}
save() {
fs.writeFile(path, JSON.stringify(this.data), err => console.dir(err, {colors: true})
}
set(key, value) {
this.data[key] = value;
this.save()
}
get(key) {
return this.data[key]
}
}
const json = new AutoSaveJSON('./some_data.json')
json.set('key', 'value')
json.get('key') === 'value'
const request = require('request');
const FeedParser = require('feedparser');
function readFeed(url) {
return new Promise((resolve, reject) => {
const $this = this;
const feedParser = new FeedParser();
const req = this.request(url);
feedParser.on('readable', function() {
var stream = this, item = stream.read();
if (item) {
//код для работы с данными
}
});
this.feedParser.on('end', () => {
resolve();
});
feedParser.on('error', reject);
req.on('response', function(res) {
req.pipe(feedParser);
});
req.on('error', reject);
});
}
readFeed('url').then(() => {
console.log('end');
});
async
$(body).on('click', '.name-class', (e) => {...});
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
<html>
<head>
<title>TEST</title>
<script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
</head>
<body>
<form id="reg">
<input type="submit"/>
</form>
<div id="select_div"><a href="#" id="select_link">Send test data</a></div>
<script type="text/javascript">
$(function(){
$('#select_link').click(function(e){
e.preventDefault();
console.log('Send test data');
var data = {
name: "Виктор",
surname: "Цой"
};
$.ajax({
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json',
url: '/reg',
success: function(data) {
console.log('success');
console.log(JSON.stringify(data));
}
});
});
});
var form = document.querySelector('#reg');
form.addEventListener('submit', event => {
console.log('Form submited');
event.preventDefault();
var json = JSON.stringify({
name: "Виктор",
surname: "Цой"
});
var xhr = new XMLHttpRequest();
xhr.open('POST', '/reg');
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.send(json);
});
</script>
</body>
</html>
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser());
app.post('/reg', function(req, res){
console.log('body: ' + JSON.stringify(req.body));
res.send(req.body);
});
app.get('/', function(req, res) {
res.sendfile('views/test.html', {root: __dirname })
});
app.listen(10240);
{
"name": "testapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.14.0",
"body-parser": "1.8"
}
}
app.post('/login', async (req, res, next) => {
const { username, password } = req.body;
try {
const user = await User.find({ username, password }).exec();
if (!user) {
return res.status(401).json({ message: 'Invalid username or password'});
}
return res.send({ success: true });
} catch(e) {
next(e);
}
});
<form method='post' action='/reg' id='reg-form></form>
var form = document.getElementById('reg-form');
form.addEventListener('submit', event => {
event.preventDefault(); // блокируем обычную отправку формы
var formData = new FormData(form);
fetch("/reg", {
method: "POST",
body: formData
})
.then(res => res.json())
.then(res => console.log(res.data)) // искомый ответ
});