Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8829

node.jsで起動したtextlintに問い合わせる

$
0
0

手抜き

nginxでnode.jsの8080ポートへ

/つけるかつけないか要注意

https://www.xmisao.com/2014/05/09/nginx-proxy-pass.html

http://example.com/name/foo
proxy_pass http://127.0.0.1/; -> http://127.0.0.1/foo
proxy_pass http://127.0.0.1; -> http://127.0.0.1/name/foo

https://stackoverflow.com/questions/33426771/nginx-proxy-pass-gives-extra-slash
- location /wp {でなくlocation /wp/ {

/etc/nginx//etc/nginx/default.d/textlint.conf
location/textlint/{proxy_redirectoff;proxy_set_headerHost$http_host;proxy_set_headerX-Forwarded-Host$http_host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;allow<許可IP>;denyall;proxy_passhttp://127.0.0.1:8080/;}

サーバー側node.js

CORS(Cross-Origin Resource Sharing)対応しておかないと問い合わせできないので許可する

https://qiita.com/chenglin/items/5e563e50d1c32dadf4c3

app.js
// vim:set ts=2 et:constTextLintEngine=require('textlint').TextLintEngine;constexpress=require('express');constcors=require('cors')constbodyParser=require('body-parser');constapp=express();// postデータのjsonをパースするおまじないapp.use(bodyParser.urlencoded({extended:true}));app.use(bodyParser.json());// allow corsapp.use(cors());// 8080番ポートで待ちうけるapp.listen(8080,()=>{console.log('Running at Port 8080...');});app.post('/api/request',(req,res,next)=>{constreq_text=req.body.text;constoptions={rules:["no-todo","max-kanji-continuous-len",],rulesConfig:{"no-todo":true,"max-kanji-continuous-len":true,},};constengine=newTextLintEngine(options);engine.executeOnText(req_text).then(results=>{res.json({messages:results[0].messages});});});// その他のリクエストに対する404エラーapp.use((req,res)=>{varurl=req.protocol+'://'+req.headers.host+req.url;console.log(url);res.sendStatus(404);});

クライアントは単純にindex.html

flask用にinputのhidden属性でURLを取得する

index.html
<!DOCTYPE html><htmllang="ja"><head><metacharset="UTF-8"><title>Title</title><linkrel="stylesheet"href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"crossorigin="anonymous"><script src="https://code.jquery.com/jquery-3.3.1.min.js"integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="crossorigin="anonymous"></script><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"crossorigin="anonymous"></script></head><body><inputtype="hidden"name="textlint_url"value="http://localhost/textlint/api/request"><form><divclass="form-group"><labelfor="lint_textarea">Example textarea</label><textareaclass="form-control"id="lint_textarea"rows="10"placeholder="Write something here..."></textarea><buttontype="button"class="btn btn-primary"id="btn_lint">Save</button></div></form><script type="text/javascript">(function(){'use strict';$('#btn_lint').on('click',function(){console.log("1");lettext=$('#lint_textarea').val();lettextData=JSON.stringify({'text':text,});console.log(textData)lettextlint_url=$('input[name=textlint_url]').val();console.log(textlint_url)$.ajax({type:'POST',url:textlint_url,data:textData,contentType:'application/json',}).done(function(data,textStatus,jqXHR){console.log(data);}).fail(function(jqXHR,textStatus,errorThrown){console.log("failed");});});}());</script></body></html>

Viewing all articles
Browse latest Browse all 8829

Trending Articles